Hibernate异常(3):org.hibernate.cache.NoCacheRegionFactoryAvailableException

本文解决了一个使用SpringSide3.2升级Hibernate到4.1版本后遇到的缓存异常问题。主要关注了配置中关于缓存区域工厂类的缺失,解释了如何正确设置该属性来解决异常。

升级Hibernate到4.1,抛出异常:

 

Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given, please either disable second level cache or set correct region factory class name to property hibernate.cache.region.factory_class (and make sure the second level cache provider, hibernate-infinispan, for example, is available in the classpath).  
    at org.hibernate.cache.internal.NoCachingRegionFactory.buildTimestampsRegion(NoCachingRegionFactory.java:87)

 

 项目是基于springside3.2升级Hibernate到4.1版本,applicationContext.xml关于Hibernate3.3的配置如下

	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="namingStrategy">
			<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider
				</prop>
				<prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}</prop>
			</props>
		</property>
		<property name="packagesToScan" value="com.carton.oa.entity.*" />
	</bean>	

	<!-- Hibernate3 事务管理器配置,单数据源事务 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

 

升级之后Hibernate的配置也需要做相应的修改,Hibernate4.1配置如下

	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="namingStrategy">
			<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory
				</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
				<prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}</prop>
			</props>
		</property>
		<property name="packagesToScan" value="com.jbase.entity.*" />
	</bean>

	<!-- Hibernate4 事务管理器配置,单数据源事务 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

 

<think>我们正在讨论Hibernate二级缓存的RegionFactory配置。用户询问是否可以将`org.hibernate.cache.jcache.JCacheRegionFactory`替换为其他实现。 根据Hibernate文档,RegionFactory是用于集成缓存提供者的核心接口。常见的替代方案包括: 1. Ehcache专用:`org.hibernate.cache.ehcache.EhCacheRegionFactory` (旧版) 或 `org.ehcache.jsr107.EhcacheCachingProvider` (通过JCache) 2. Infinispan:`org.hibernate.cache.infinispan.InfinispanRegionFactory` 3. Hazelcast:`org.hibernate.cache.hazelcast.HazelcastRegionFactory` 4. Redis:需要第三方实现(如redis-hibernate-cache) 但注意:使用`JCacheRegionFactory`是符合JSR-107标准的通用方式,只要正确配置JCache提供者(如Ehcache、Infinispan等),它就能工作。因此,替换不是必须的,但可以根据缓存实现选择专用工厂。 建议:除非有特殊需求(如需要特定缓存提供者的高级功能),否则使用标准的`JCacheRegionFactory`并正确配置JCache提供者即可,这样更通用。 配置示例(Ehcache): ```yaml spring: jpa: properties: hibernate: cache: region.factory_class: org.hibernate.cache.jcache.JCacheRegionFactory javax: persistence: sharedCache: mode: ENABLE_SELECTIVE # 指定JCache提供者(Ehcachehibernate.javax.cache.provider: org.ehcache.jsr107.EhcacheCachingProvider hibernate.javax.cache.uri: classpath:ehcache.xml ``` 如果不想用JCache标准,可以直接用Ehcache的专用工厂(但较旧): ```yaml region.factory_class: org.hibernate.cache.ehcache.EhCacheRegionFactory ``` 但注意:Hibernate 5.3+ 推荐使用JCache标准。 因此,回答是:可以换,但建议保持`JCacheRegionFactory`,除非有明确理由使用特定实现。</think>### `region.factory_class` 配置解析 可以更换 `org.hibernate.cache.jcache.JCacheRegionFactory`,具体取决于您选择的缓存实现。以下是常见替代方案: #### 可选 RegionFactory 实现 | **缓存实现** | **RegionFactory 类名** | **适用场景** | |--------------|------------------------|-------------| | Ehcache (原生) | `org.hibernate.cache.ehcache.EhCacheRegionFactory` | 直接集成 Ehcache | | Infinispan | `org.hibernate.cache.infinispan.InfinispanRegionFactory` | 分布式缓存场景 | | Hazelcast | `org.hibernate.cache.hazelcast.HazelcastRegionFactory` | 内存网格计算 | | Caffeine | `com.github.benmanes.caffeine.jcache.spi.CaffeineCachingProvider` | 高性能本地缓存 | | Redis | `org.redisson.hibernate.RedissonRegionFactory` | 集中式缓存管理 | #### 配置示例(Ehcache 原生集成) ```yaml jpa: properties: hibernate: cache: region.factory_class: org.hibernate.cache.ehcache.EhCacheRegionFactory use_second_level_cache: true use_query_cache: false ``` #### 更换注意事项 1. **依赖需同步变更**: - 使用 Ehcache 原生:移除 `hibernate-jcache` 依赖 - 使用 Redis:添加 Redisson 依赖 ```xml <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-hibernate-53</artifactId> </dependency> ``` 2. **配置差异**: - JCache 使用统一配置(`javax.cache`) - 原生实现需单独配置缓存策略文件(如 `ehcache.xml`) 3. **性能影响**: - 原生集成通常性能更高(减少抽象层) - JCache 标准更易切换实现 > **建议**:若无特殊需求,建议保持 `JCacheRegionFactory` + Ehcache 组合,这是 Spring Boot 官方推荐方案([参考文档](https://docs.spring.io/spring-boot/docs/current/reference/html/data.html#data.jpa.hibernate-cache))。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值