【问题标题】:EhCache + Hibernate Cache is not aliveEhCache + Hibernate Cache 不存在
【发布时间】:2011-11-30 14:12:08
【问题描述】:

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

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>

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

【问题讨论】:

  • 你不妨将 hibernate.cache.provider_class 属性设置为 net.sf.ehcache.hibernate.EhCacheProvider
  • 您将 ehcache.xml 文件引用为“/ehcache.xml”是有原因的,Hibernate 是否会选择它?如果它们在同一个类路径中,则不必引用它。
  • 我也尝试过 EhCacheProvider 存在同样的问题,关于 ehcache.xml 你是对的,hibernate 会选择它,所以虽然它是多余的,但它不会造成任何伤害。
  • @Saffar :我也遇到了同样的问题。但仅在特定情况下:在 VM 中运行的 Jenkins 实例 - VMWare ESXi (v4.0.0)。它不会发生在我的本地环境中。它也不会发生在 VMWare Player VM 中。 (休眠 3.6.9.Final EHCache 2.4.6)。你发现什么问题了吗?
  • 我在这里遇到了同样的问题。但是在网上还没有找到解决方案。有人吗?

标签: hibernate caching ehcache second-level-cache


【解决方案1】:

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

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

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

祝你好运。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我也不知道真正的原因。但我猜测这可能是由于应用程序类路径中有多个不兼容的 EhCache 版本而发生的。通过使用以下 maven 命令检查依赖树来检查这一点:

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

      (PowerShell 语法)

      【讨论】:

        猜你喜欢
        • 2012-08-07
        • 2017-01-11
        • 1970-01-01
        • 1970-01-01
        • 2010-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多