简体   繁体   English

JBossCache作为JBoss 5中Hibernate的二级缓存

[英]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. 这是我的配置:Hibernate 3.3.1.GA,JBoss 5.1.0.GA,JBoss Cache3.2.0.GA。

I'm doing Hibernate configuration as described here: http://www.jboss.org/community/wiki/ClusteredJPAHibernateSecondLevelCachinginJBossAS5 我正在按此处所述进行Hibernate配置: 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). 因此,我不能使用“ cache.region.factory_class”属性,而只能使用“ cacheRegionFactoryClass”(在异常中显示)。

I can not use any other properties like cache.region.* and thus can not configurate second level cache for my hibernate. 我不能使用任何其他属性(例如cache.region。*),因此无法为我的休眠配置二级缓存。

Can anyone give me a link how to configurate JBoss Cache 3.2 with JBoss 5.1? 谁能给我链接如何用JBoss 5.1配置JBoss Cache 3.2? I'm especially interested in JndiSharedJBossCacheRegionFactory and JndiMultiplexedJBossCacheRegionFactory. 我对JndiSharedJBossCacheRegionFactory和JndiMultiplexedJBossCacheRegionFactory尤其感兴趣。

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, ie put hibernate configuration file into deploy folder of the JBoss server. 事实证明,如果您以mbean启动Hibernate,即无法将Jibers配置文件放入JBoss服务器的deploy文件夹,则无法在JBoss 5.1中将JBoss Cache与Hibernate一起使用。

This happens because mbean does not accept parameters like "hibernate.cache.*" (and that is exactly what exception is about). 发生这种情况是因为mbean不接受“ hibernate.cache。*”之类的参数(而这正是异常所在)。

So my solution is to initialize Hibernate from java code and get ride of hibernate.xml. 所以我的解决方案是从Java代码初始化Hibernate并获得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, those properties are new properties that haven't been mapped to hibernate MBean attributes because the Hibernate MBean is no longer maintained. @Yury Litvinov,这些属性是尚未映射到休眠MBean属性的新属性,因为不再维护休眠MBean。 I wouldn't recommend that you deploy Hibernate as an MBean. 我不建议您将Hibernate部署为MBean。

After some investigation I managed to start Hibernate+JBossCache with this configuration. 经过一番调查后,我设法使用此配置启动了Hibernate + JBossCache。

<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". 但是,我仍然不能指定以下参数(出现相同的错误):“ 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. 如果不指定此参数,则无法控制将哪个缓存实例用于缓存条目,集合和查询。

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. 我看了一下,得出的结论是,用于解析和部署hibernate.cfg.xml文件的JBoss AS机制过于脆弱,相对于Hibernate支持的配置选项而言,它很容易过时。 I've opened https://jira.jboss.org/jira/browse/JBAS-7411 with a suggestion of a possible way to improve this. 我已经打开https://jira.jboss.org/jira/browse/JBAS-7411 ,并提出了一种改善此问题的可行方法的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM