JBossCache as second level cache for Hibernate in JBoss 5
Here is my configuration: Hibernate 3.3.1.GA, JBoss 5.1.0.GA, JBoss Cache 3.2.0.GA.
I am doing Hibernate config as described here: http://www.jboss.org/community/wiki/ClusteredJPAHibernateSecondLevelCachinginJBossAS5
<hibernate-configuration>
<session-factory>
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache">true</property>
<property name="cache.region.factory_class">org.hibernate.cache.jbc2.JndiMultiplexedJBossCacheRegionFactoryctory</property>
<property name="cache.region.jbc2.cachefactory>java:CacheManager</property>
<property name="cache.region.jbc2.cfg.entity">mvcc-entity</property>
<property name="cache.region.jbc2.cfg.query">local-query</property>
<property name="cache.region_prefix">tempdb</property>
... other non-caching related configuration
</session-factory>
</hibernate-configuration>
but getting error, the specified property specified is invalid:
Caused by: java.lang.IllegalArgumentException: No such property cache for bean org.jboss.hibernate.jmx.Hibernate available [statisticsServiceName, beanName, defaultSchema, defaultCatalog, sessionFactoryName, querySubstitutions, secondLevelCacheEnabled, password, version, statGenerationEnabled, maxFetchDepth, username, useStructuredCacheEntriesEnabled, datasourceName, dirty, streamsForBinaryEnabled, getGeneratedKeysEnabled, hbm2ddlAuto, minimalPutsEnabled, instance, jdbcBatchSize, jdbcScrollableResultSetEnabled, cacheRegionFactoryClass, dialect, scanForMappingsEnabled, runningSince, cacheRegionPrefix, class, cacheProviderClass, sessionFactoryRunning, batchVersionedDataEnabled, harUrl, queryCacheEnabled, sessionFactoryInterceptor, deployedCacheManagerJndiName, showSqlEnabled, reflectionOptimizationEnabled, jdbcFetchSize, listenerInjector, sqlCommentsEnabled, deployedCacheJndiName, controller]
So, I cannot use the cache.region.factory_class property, but only "cacheRegionFactoryClass" (which is shown as an exception).
I cannot use other properties like cache.region. * and hence cannot configure the L2 cache for my hibernate.
Can anyone give me a link on how to set up JBoss Cache 3.2 with JBoss 5.1? I am particularly interested in JndiSharedJBossCacheRegionFactory and JndiMultiplexedJBossCacheRegionFactory.
source to share
Answering my own question.
It turned out that you cannot use JBoss Cache with Hibernate in JBoss 5.1 if you start Hibernate as mbean, i.e. put the hibernate config file in your JBoss server deployment folder.
This is because mbean does not accept parameters such as "hibernate.cache. *" (Which is exactly what it is about).
So my solution is to initialize Hibernate from java code and access hibernate.xml.
Configuration configuration = new Configuration();
Properties properties = configuration.getProperties();
properties.put("hibernate.connection.datasource", "java:/MSSQLDMDS");
properties.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
properties.put("hibernate.transaction.factory_class", "org.hibernate.transaction.JTATransactionFactory");
properties.put("hibernate.current_session_context_class", "org.hibernate.context.JTASessionContext");
properties.put("hibernate.transaction.manager_lookup_class", "org.hibernate.transaction.JBossTransactionManagerLookup");
properties.put("hibernate.cache.use_second_level_cache", "true");
properties.put("hibernate.cache.use_query_cache", "false");
properties.put("hibernate.cache.region.factory_class", "org.hibernate.cache.jbc2.JndiMultiplexedJBossCacheRegionFactory");
properties.put("hibernate.cache.region.jbc2.cachefactory", "java:CacheManager");
properties.put("hibernate.cache.region.jbc2.cfg.entity", "mvcc-entity");
File mappings = getHibernateMappingDir();
configuration.addDirectory(mappings);
sessionFactory = configuration.buildSessionFactory();
source to share
After some research, I was able to start Hibernate + JBossCache with this configuration.
<hibernate-configuration xmlns="urn:jboss:hibernate-deployer:1.0">
<session-factory name="java:/hibernate/SessionFactory" bean="jboss.har:service=Hibernate">
<property name="datasourceName">java:/MSSQLDMDS</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hbm2ddlAuto">create</property>
<property name="secondLevelCacheEnabled">true</property>
<property name="queryCacheEnabled">false</property>
<property name="cacheProviderClass">org.hibernate.cache.jbc2.JndiMultiplexedJBossCacheRegionFactory</property>
<property name="deployedCacheManagerJndiName">java:CacheManager</property>
<depends>jboss.cache:service=CacheManager</depends>
<depends>jboss:service=Naming</depends>
<depends>jboss:service=TransactionManager</depends>
</session-factory>
</hibernate-configuration>
However, I still cannot specify (get the same error) the following parameters: "hibernate.cache.region.jbc2.cfg.entity", "hibernate.cache.region.jbc2.cfg.collection", "hibernate.cache.region .jbc2.cfg.query ".
Without specifying these parameters, I have no control over which instance of the cache will be used to cache records, collections, and queries.
source to share
I looked at this and concluded that the JBoss AS engine for parsing and deploying the hibernate.cfg.xml file is overly fragile and deprecated regarding the configuration options that Hibernate supports. I opened up https://jira.jboss.org/jira/browse/JBAS-7411 with a suggestion for a possible way to improve this.