hibernate - EhCache + Hibernate Cache 不存在

标签 hibernate caching ehcache second-level-cache

在将 EhCache v2.4.5 配置为 hibernate v3.6.7 的二级缓存后,我在尝试使用 hibernate session 加载特定实体的所有对象时收到以下错误。 (第一次加载对象没有错误)

java.lang.IllegalStateException: The country Cache is not alive.
at net.sf.ehcache.Cache.checkStatus(Cache.java:2438)
at net.sf.ehcache.Cache.get(Cache.java:1541)
at net.sf.ehcache.hibernate.regions.EhcacheTransactionalDataRegion.get(EhcacheTransactionalDataRegion.java:105)
at net.sf.ehcache.hibernate.strategy.AbstractReadWriteEhcacheAccessStrategy.putFromLoad(AbstractReadWriteEhcacheAccessStrategy.java:90)
at net.sf.ehcache.hibernate.nonstop.NonstopAwareEntityRegionAccessStrategy.putFromLoad(NonstopAwareEntityRegionAccessStrategy.java:180)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:195)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:982)
at org.hibernate.loader.Loader.doQuery(Loader.java:857)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.doList(Loader.java:2533)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
at dataAccess.CountryDAO.loadAll(CountryDAO.java:80)

我的 hibernate 配置是:
<property name="hibernate.cache.region.factory_class">
        net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
</property>
<property name="hibernate.cache.provider_configuration">
    /ehcache.xml
</property>
<property name="hibernate.cache.use_second_level_cache">
    true
</property>
<property name="hibernate.cache.use_query_cache">
    true
</property>

我的 EhCache 配置是:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true">

<diskStore path="java.io.tmpdir" />

<transactionManagerLookup
    class="net.sf.ehcache.transaction.manager.DefaultTransactionManagerLookup"
    properties="jndiName=java:/TransactionManager" propertySeparator=";" />

<cacheManagerEventListenerFactory
    class="" properties="" />

<defaultCache maxElementsInMemory="0" eternal="false"
    overflowToDisk="true" timeToIdleSeconds="1200" timeToLiveSeconds="1200">
</defaultCache>
<cache name="country"
    maxElementsInMemory="300" eternal="false" overflowToDisk="false"
    timeToIdleSeconds="12000" timeToLiveSeconds="12000" diskPersistent="false"
    diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
<cache name="city"
    maxElementsInMemory="300" eternal="false" overflowToDisk="false"
    timeToIdleSeconds="12000" timeToLiveSeconds="12000" diskPersistent="false"
    diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
</ehcache>

我不知道出了什么问题。有任何想法吗?

最佳答案

我不知道发生这种情况的实际原因,但我有一个跟踪它的建议。

查看 net.sf.ehcache.Cache 的源代码,可以看到“事件”检查只是检查内存中的标志,Cache.cacheStatus .在调试器下运行代码应该很简单,并在 Cache.dispose() 中设置断点。 (其中状态设置为 Status.STATUS_SHUTDOWN )甚至在 Cache.CacheStatus.changeState() 中(捕捉任何状态转换)。

您可能会发现 Hibernate 实际上存在一个错误,或者它可能是您的使用。无论哪种方式,您都应该能够准确地找到将缓存状态更改为“不存在”的人。

祝你好运。

关于hibernate - EhCache + Hibernate Cache 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7646651/

相关文章:

mysql - NamedParameterJdbcTemplate.update 如何真正与 Spring 和 MySQL 一起工作

mysql - Tomcat/hibernate 配置

date - elasticsearch 中的日期舍入如何工作,它如何影响缓存流失?

java - 从实体的服务层调用另一个实体的存储库

java - 平板电脑上 api 22(5.1.1) 的 picasso 缓存错误

django - 如何缓存 FileField 中的文件以避免重复(Django、DRF)

spring - Ehcache 不允许持久化元素

java - EHCache 如何检查缓存中是否有内容?

caching - Play 2.4 测试出错 : The CacheManager has been shut down. 无法再使用

java - Spring MVC 和应用程序上下文多次刷新尝试