gpt4 book ai didi

spring hibernate 和使用 ehcache 进行缓存

转载 作者:行者123 更新时间:2023-12-02 03:17:34 24 4
gpt4 key购买 nike

我正在使用 spring 3.2 和 hibernate 4.2.8 并尝试使用 ehcache,但是当我用 @Cache(org.hibernate.annotations.Cache) 注释实体类时,我得到以下异常

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.buildEntityRegion(NoCachingRegionFactory.java:69)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:351)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1797)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1868)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:247)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:373)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:358)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1547)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1485)
... 42 more

我的配置如下图

<bean id="sessionFactoryAdmin"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="adminDataSource" />
<property name="packagesToScan" value="com.digilegal.services.ahc.model.user" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.provider_configuration">/WEB-INF/ehcache-entity.xml</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
</bean>

我已经尝试了互联网上所有可能的链接,但无法解决问题

这是我的/WEB-INF/ehcache-entity.xml

    <?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<cache name="com.digilegal.services.ahc.model.user.UserNotifications"
maxElementsInMemory="500"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="69"
timeToLiveSeconds="65" />


<cache name="org.hibernate.cache.StandardQueryCache"
maxEntriesLocalHeap="15" eternal="false" timeToLiveSeconds="60"
overflowToDisk="true" />
</ehcache>

谢谢​尼拉夫

最佳答案

我最近遇到了这个错误,在我的例子中,错误发生在使用不同 session 工厂配置的测试中,而没有指定 hibernate.cache.region.factory_class

因此,@Cache 注释的存在导致测试中止并出现完全相同的错误。就我而言,我不想缓存该特定 session 工厂,因此我添加了:

<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>

检查一下您的配置,看看您是否没有定义多个 session 工厂,并且有些没有提供 hibernate.cache.region.factory_class

关于spring hibernate 和使用 ehcache 进行缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23019247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com