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

标签 hibernate jboss5.x second-level-cache jboss-cache

这是我的配置:Hibernate 3.3.1.GA、JBoss 5.1.0.GA、JBoss Cache 3.2.0.GA。

我正在按照此处所述进行 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>

但收到指定属性无效的错误:

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]

因此,我不能使用“cache.region.factory_class”属性,而只能使用“cacheRegionFactoryClass”(在异常中显示)。

我无法使用任何其他属性,例如cache.region.*,因此无法为我的 hibernate 配置二级缓存。

任何人都可以给我一个如何使用 JBoss 5.1 配置 JBoss Cache 3.2 的链接吗?我对 JndiSharedJBossCacheRegionFactory 和 JndiMultiplexedJBossCacheRegionFactory 特别感兴趣。

最佳答案

回答我自己的问题。

事实证明,如果您将 Hibernate 作为 mbean 启动,即将 Hibernate 配置文件放入 JBoss 服务器的部署文件夹中,则无法在 JBoss 5.1 中将 JBoss Cache 与 Hibernate 一起使用。

发生这种情况是因为 mbean 不接受像“hibernate.cache.*”这样的参数(这正是异常的含义)。

所以我的解决方案是从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();

关于hibernate - JBossCache 作为 JBoss 5 中 Hibernate 的二级缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1443192/

相关文章:

java - 如何测量JBOSS 7服务器的启动时间?

java - 如何从 Maven 项目在 JBoss 上运行 Talend 作业?

java - 是否可以手动将值添加到 Hibernate 查询缓存映射?

hibernate - 我们可以使用 JPA2 注释来缓存关联吗?

java - Hibernate 搜索手动索引

java - 如何在JPA中有效地截断多对多表?

java - liferay钩子(Hook)部署-事件类未找到异常

java - 如何在Hibernate中级联删除?

java - 不提交 Hibernate Select 事务的后果