EhCache + Hibernate 缓存不存在
将 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)
我的休眠配置是:
<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>
我不知道出了什么问题。有什么想法吗?
After configuring EhCache v2.4.5 as the second level cache for hibernate v3.6.7 I get the following error while trying to load all objects of a particular entity using hibernate session. (There is no error for loading the objects for the first time)
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)
My hibernate configuration is:
<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>
My EhCache configuration is:
<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>
I have no clue what is going wrong. any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道发生这种情况的真正原因,但我有一个追踪它的建议。
查看
net.sf.ehcache.Cache
的源代码,我们可以看到“alive”检查只是检查内存中的标志Cache.cacheStatus
。在调试器下运行代码应该很简单,并在 Cache.dispose() 中设置断点(其中状态设置为 Status.STATUS_SHUTDOWN ),甚至在Cache.CacheStatus.changeState()
(捕获任何状态转换)。您可能会发现 Hibernate 实际上存在一个错误,或者可能是您的使用方式造成的。无论哪种方式,您都应该能够准确地找到谁将缓存状态更改为“不活动”。
祝你好运。
I don't know the actual reason why this is happening, but I have a suggestion for tracking it down.
Looking at the source code for
net.sf.ehcache.Cache
, one can see that the 'alive' check just checks an in-memory flag,Cache.cacheStatus
. It should be straightforward to run your code under the debugger, and set a breakpoint either inCache.dispose()
(where status is set toStatus.STATUS_SHUTDOWN
) or even inCache.CacheStatus.changeState()
(to catch any status transition).You may find that there is in fact a bug with Hibernate, or it may be your usage. Either way, you should be able to find exactly who's changing the state of your cache to 'not alive'.
Good luck.
我不知道 hibernate ehcache 实现具有此属性,但是当我使用 ehcache 的开箱即用解决方案时,我遇到了同样的问题,并通过在
EhCacheManagerFactoryBean
或EhCacheCacheManager
中将共享属性设置为 false 来解决豆子。I don't know hibernate ehcache implementation has this property but when i used outofbox solution of ehcache i got same problem and solved with setting up shared property to false in
EhCacheManagerFactoryBean
orEhCacheCacheManager
beans.我也不知道真正的原因。但我猜测这可能是由于应用程序类路径中存在多个不兼容的 EhCache 版本造成的。通过使用以下 Maven 命令检查依赖关系树来检查这一点:
(PowerShell 语法)
I don't know the actual reason either. But I have a guess that it may happen due to multiple incompatible versions of EhCache in application classpath. Check this by inspecting dependency tree with the following maven command:
(PowerShell syntax)