views:

2247

answers:

4

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.

A: 

After some investigation I managed 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'm still can not specify (getting the same error) following parameters: "hibernate.cache.region.jbc2.cfg.entity", "hibernate.cache.region.jbc2.cfg.collection", "hibernate.cache.region.jbc2.cfg.query".

Without specifying this parameters I can not control what cache instance will be used for caching entries, collections and queries.

Yury Litvinov
+1  A: 

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();
Yury Litvinov
A: 

@Yury Litvinov, those properties are new properties that haven't been mapped to hibernate MBean attributes because the Hibernate MBean is no longer maintained. I wouldn't recommend that you deploy Hibernate as an MBean.

Galder Zamarreño
A: 

I've had a look at this and have come to the conclusion that the JBoss AS mechanism for parsing and deploying a hibernate.cfg.xml file is overly fragile and prone to falling out of date with respect to configuration options that Hibernate supports. I've opened https://jira.jboss.org/jira/browse/JBAS-7411 with a suggestion of a possible way to improve this.

Brian Stansberry