2011-10-04 80 views
14

將EhCache v2.4.5配置爲休眠v3.6.7的第二級緩存後,嘗試使用休眠會話加載特定實體的所有對象時出現以下錯誤。 (沒有錯誤加載對象首次)EhCache +休眠緩存未生效

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> 

我不知道是什麼出錯了。有任何想法嗎?

+0

你不妨將hibernate.cache.provider_class屬性設置爲net.sf.ehcache.hibernate.EhCacheProvider – frictionlesspulley

+0

是否有原因將你的ehcache.xml文件引用爲「/ehcache.xml」,是Hibernate撿起來?如果他們在同一個類路徑中,則不必引用它。 – artgon

+0

我也試過EhCacheProvider存在同樣的問題,關於ehcache.xml你是對的,休眠選取它,所以它不會造成任何傷害,儘管它是多餘的。 – Saffar

回答

4

我不知道爲什麼會發生這種情況的實際原因,但我有一個跟蹤它的建議。

查看net.sf.ehcache.Cache的源代碼,可以看到'alive'檢查只檢查內存中的標誌Cache.cacheStatus。在調試器下運行代碼應該很簡單,並且在Cache.dispose()(其中狀態設置爲Status.STATUS_SHUTDOWN)或者甚至在Cache.CacheStatus.changeState()(以捕獲任何狀態轉換)中設置斷點。

您可能會發現實際上有一個Hibernate的錯誤,或者它可能是您的使用。無論哪種方式,您都應該能夠準確找到誰將您的緩存狀態更改爲「不活躍」。

祝你好運。

0

我不知道hibernate的ehcache實現有這個屬性,但是當我使用ehcache的outofbox解決方案時,我得到了同樣的問題,並在EhCacheManagerFactoryBeanEhCacheCacheManager beans中將共享屬性設置爲false。