【问题标题】:hibernate 4.x with second level Ehcache not working as expected具有二级 Ehcache 的休眠 4.x 未按预期工作
【发布时间】:2014-05-13 17:54:15
【问题描述】:

这是我的配置:

pom.xml: 使用hibernate和hibernate-ehcache 4.2.8.Final版本

spring config:我有以下休眠属性

<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>

ehcache.xml

<ehcache>

    <diskStore path="java.io.tmpdir"/>

    <!--  cache setting for com.cisco.locker.entity.SystemConfig entity -->
    <cache name="com.cisco.locker.entity.SystemConfig"
        maxElementsInMemory="100"
        eternal="false"
        timeToIdleSeconds="600"
        timeToLiveSeconds="600"
        overflowToDisk="false"
        statistics="true"
        />

</ehcache>

实体 - com.cisco.locker.entity.SystemConfig.java

@Entity
@Table(name = "system_config")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class SystemConfig implements Serializable {
...
}

日志:

First call: http://localhost:8080/test/getSystemConfigs
{
    "statusCode": "200",
    "statusMessage": "SUCCESS",
    "response": {
        "admin.lcu_status_polling_timer": "120",
        "admin.site_status_polling_interval": "15"
    }
}

Hibernate: select systemconf0_.ID as ID1_12_, systemconf0_.description as descript2_12_, systemconf0_.property as property3_12_, systemconf0_.value as value4_12_ from system_config systemconf0_ where systemconf0_.property like ?
2014-05-13 00:53:40,553 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-1]: com.cisco.locker.entity.SystemConfig cache - Miss
2014-05-13 00:53:40,561 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-1]: com.cisco.locker.entity.SystemConfig cache - Miss

Second call: http://localhost:8080/test/getSystemConfigs
{
    "statusCode": "200",
    "statusMessage": "SUCCESS",
    "response": {
        "admin.lcu_status_polling_timer": "120",
        "admin.site_status_polling_interval": "15"
    }
}

Hibernate: select systemconf0_.ID as ID1_12_, systemconf0_.description as descript2_12_, systemconf0_.property as property3_12_, systemconf0_.value as value4_12_ from system_config systemconf0_ where systemconf0_.property like ?
2014-05-13 00:53:48,037 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-2]: com.cisco.locker.entity.SystemConfigCache: com.cisco.locker.entity.SystemConfig store hit for com.cisco.locker.entity.SystemConfig#11
2014-05-13 00:53:48,037 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-2]: com.cisco.locker.entity.SystemConfigCache: com.cisco.locker.entity.SystemConfig store hit for com.cisco.locker.entity.SystemConfig#12


Third call – After manually changing value in DB "admin.site_status_polling_interval" from 15 to 10: http://localhost:8080/test/getSystemConfigs
{
    "statusCode": "200",
    "statusMessage": "SUCCESS",
    "response": {
        "admin.lcu_status_polling_timer": "120",
        "admin.site_status_polling_interval": "10"
    }
}

Hibernate: select systemconf0_.ID as ID1_12_, systemconf0_.description as descript2_12_, systemconf0_.property as property3_12_, systemconf0_.value as value4_12_ from system_config systemconf0_ where systemconf0_.property like ?
2014-05-13 00:54:08,738 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-4]: com.cisco.locker.entity.SystemConfigCache: com.cisco.locker.entity.SystemConfig store hit for com.cisco.locker.entity.SystemConfig#11
2014-05-13 00:54:08,738 DEBUG net.sf.ehcache.Cache [http-bio-8080-exec-4]: com.cisco.locker.entity.SystemConfigCache: com.cisco.locker.entity.SystemConfig store hit for com.cisco.locker.entity.SystemConfig#12

当我最终进行手动数据库更改时,我似乎不明白缓存是如何获取最新值的。日志表明它也在从缓存中挑选这些更改的值。

二级缓存是否会接收手动 DB 更改,即 DB 更改不会像上面的日志所暗示的那样经过休眠?还是我做错了什么?

【问题讨论】:

    标签: java spring hibernate ehcache


    【解决方案1】:

    嗯,正如日志所示,对数据库执行 SQL 查询。数据来自数据库,而不是缓存。您看到的点击量是商店点击量。

    要从缓存中获取实体,请使用session.get(),而不是查询。

    【讨论】:

    • JB Nizet... api 在内部使用 session.get()。由于我设置了 hibernate.show_sql=true 属性,因此您在日志中看到了 SQL 查询。另外,“您看到的点击是商店点击”到底是什么意思,这是否意味着它正在从缓存中获取值?感谢您的帮助!
    • 所有 where 子句都是where systemconf0_.property like ?。这不可能是session.get() 的结果。对我来说,存储命中是指某些东西存储在缓存中。没有从缓存中得到。
    • 你说得对,我使用的是 HQL 查询而不是 session.get()。让我尝试使用 session.get()。
    猜你喜欢
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 2011-05-19
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多