Search code examples
hibernatejboss5.xsecond-level-cachejboss-cache

JBossCache as a 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'm doing Hibernate configuration 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 that specified property 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 can not use "cache.region.factory_class" property but only "cacheRegionFactoryClass" (which is shown in exception).

I can not use any other properties like cache.region.* and thus can not configurate second level cache for my hibernate.

Can anyone give me a link how to configurate JBoss Cache 3.2 with JBoss 5.1? I'm especially interested in JndiSharedJBossCacheRegionFactory and JndiMultiplexedJBossCacheRegionFactory.


Solution

  • Answering to 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 hibernate configuration file into deploy folder of the JBoss server.

    This happens because mbean does not accept parameters like "hibernate.cache.*" (and that is exactly what exception is about).

    So my solution is to initialize Hibernate from java code and get ride of 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();