EhCache + Hibernate Cache不可用。

18
在将EhCache v2.4.5配置为Hibernate v3.6.7的二级缓存后,我尝试使用Hibernate会话加载特定实体的所有对象时出现以下错误。(第一次加载对象时没有错误)
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>

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


你可以将 hibernate.cache.provider_class 属性设置为 net.sf.ehcache.hibernate.EhCacheProvider。 - frictionlesspulley
我也尝试了EhCacheProvider,但是同样的问题存在。关于ehcache.xml,你是正确的,Hibernate会自动获取它,所以它不会造成任何损害,尽管它是多余的。 - Saffar
2
我这里也遇到了同样的问题。但是网上还没有找到解决方案。有人知道吗? - JordyOnrust
我在Hudson上遇到了同样的问题。 - Luigi Saggese
只需重新启动服务器即可解决问题。当空闲时间超过XML文件中给定的值时,就会出现这种情况。 - Lucky
显示剩余5条评论
3个回答

6

我不知道为什么会发生这种情况,但我有一个跟踪它的建议。

查看net.sf.ehcache.Cache的源代码,可以看到'alive'检查只是检查内存标志Cache.cacheStatus。 在调试器下运行您的代码并设置断点应该很容易,在Cache.dispose()(在此处状态设置为Status.STATUS_SHUTDOWN)或甚至在Cache.CacheStatus.changeState()中设置断点(以捕获任何状态转换)。

您可能发现实际上存在Hibernate的错误,或者可能是您的使用方式。 无论哪种方式,您都应该能够找到将您的缓存状态更改为“未激活”的确切人员。

祝你好运。


0

我也不知道确切的原因。但是我猜测可能是由于应用程序类路径中存在多个不兼容版本的EhCache导致的。通过以下Maven命令检查依赖树来验证:

mvn dependency:tree '-Dincludes=*:*cache*'

(PowerShell 语法)


0

我不知道hibernate ehcache实现是否具有此属性,但当我使用ehcache的开箱即用解决方案时,我遇到了同样的问题,并通过在EhCacheManagerFactoryBeanEhCacheCacheManager bean中设置共享属性为false来解决了这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接