【问题标题】:hibernate second level cache keeps emptying itself休眠二级缓存不断清空自己
【发布时间】:2017-10-07 00:22:25
【问题描述】:

有一个 Spring/Tomcat 应用程序,我正在尝试让 ehcache 作为 Hibernate 二级实体缓存工作。它已启用并处于活动状态。我可以在调试日志输出中看到缓存了多少实体。问题是每隔几秒钟,缓存就会清空。我什至固定了缓存(只是想看看这是否会改变行为),现在我看到了:

05-08-2017 16:05:21.550 [taskExecutor-12] {env=''} WARN  n.s.e.Cache: Data availability impacted:
****************************************************************************************
************************** removeAll called on a pinned cache **************************

05-08-2017 16:05:21.550 [taskExecutor-12] {env=''} WARN  n.s.e.Cache: Data availability impacted:
****************************************************************************************
************************** removeAll called on a pinned cache **************************

所以我认为这是问题所在。知道什么会导致 removeAll 被一遍又一遍地隐式调用吗?

ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd"
    updateCheck="true"
    monitoring="autodetect"
    maxBytesLocalHeap="24G"
    dynamicConfig="true">

    <defaultCache eternal="true" maxBytesLocalHeap="1" /> <!-- not used, but required per ehcache configuration rules -->

    <!-- cache for foo -->
    <cache name="foo"
           maxBytesLocalHeap="8G"
           eternal="false"
           memoryStoreEvictionPolicy="LRU"
           timeToLiveSeconds="12000"
           transactionalMode="off">
        <pinning store="inCache" />
        <persistence strategy="none" />
    </cache>
</ehcache>

persistence.xml

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" />
            <property name="hibernate.cache.use_second_level_cache" value="true" />
            <property name="hibernate.cache.use_query_cache" value="false" />
            <property name="hibernate.generate_statistics" value="true" />
            <property name="hibernate.cache.use_structured_entries" value="true" />
            <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" />
        </properties>

并且这些注解在实体模型类上:

@Cacheable
@Cache(region = "foo", usage = CacheConcurrencyStrategy.READ_WRITE)

【问题讨论】:

    标签: spring hibernate ehcache second-level-cache


    【解决方案1】:

    我想通了。本机 SQL DELETE 查询导致 hibernate 频繁转储整个缓存(上面的 removeAll 调用)。我按照下面的建议做了,现在一切正常。

    2nd level cache invalidation with native queries

    【讨论】:

      猜你喜欢
      • 2017-07-05
      • 2010-10-20
      • 2012-10-12
      • 2011-07-08
      • 1970-01-01
      • 2016-06-23
      • 2011-12-27
      • 1970-01-01
      相关资源
      最近更新 更多