为什么Hibernate二级静态显示与CacheManager不同的值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么Hibernate二级静态显示与CacheManager不同的值相关的知识,希望对你有一定的参考价值。

使用Hibernate 4.3.11和Ehcache 2.7.0

为什么Hibernates getStatistics()返回的命中/未命中与CacheManager返回的值不匹配。我为单个类启用了第二级缓存。

所以用这个代码

Statistics stats = HibernateUtil.getFactory().getStatistics();
    writeToBothLogs("Report:" + currentReportId + ":DbConnect:" + stats.getConnectCount());
    writeToBothLogs("Report:" + currentReportId + ":DbQueries:" + stats.getQueryExecutionCount());
    writeToBothLogs("Report:" + currentReportId + ":2ndLevelPut:" + stats.getSecondLevelCachePutCount());
    writeToBothLogs("Report:" + currentReportId + ":2ndLevelHit:" + stats.getSecondLevelCacheHitCount());
    writeToBothLogs("Report:" + currentReportId + ":2ndLevelMiss:" + stats.getSecondLevelCacheMissCount());


    String[] cacheNames = CacheManager.getInstance().getCacheNames();
    for(String cacheName:cacheNames)
    {
        SecondLevelCacheStatistics secondLeveleStats = stats.getSecondLevelCacheStatistics(cacheName);
        writeToBothLogs("Report:" + currentReportId + ":"+cacheName+":2ndLevelPut:" +  secondLeveleStats.getPutCount());
        writeToBothLogs("Report:" + currentReportId + ":"+cacheName+":2ndLevelHit:" + secondLeveleStats.getHitCount());
        writeToBothLogs("Report:" + currentReportId + ":"+cacheName+":2ndLevelMiss:" + secondLeveleStats.getMissCount());
        writeToBothLogs("Report:" + currentReportId + ":"+cacheName+":2ndLevelSizeInMemory:" + secondLeveleStats.getSizeInMemory());

        Cache cache = CacheManager.getInstance().getCache(cacheName);
        writeToBothLogs("CacheName:"+cacheName
                +":Put:"+cache.getStatistics().cachePutCount()
                +":Hit:"+cache.getStatistics().cacheHitCount()
                +":Miss:"+cache.getStatistics().cacheMissCount()
                +":Size:"+cache.getSize()
        );

    }

    writeToBothLogs("Report:" + currentReportId + ":DeleteCount:" + stats.getEntityDeleteCount());
    writeToBothLogs("Report:" + currentReportId + ":InsertCount:" + stats.getEntityInsertCount());
    writeToBothLogs("Report:" + currentReportId + ":FetchCount:" + stats.getEntityFetchCount());
    writeToBothLogs("Report:" + currentReportId + ":UpdateCount:" + stats.getEntityUpdateCount());
    writeToBothLogs("Report:" + currentReportId + ":LoadCount:" + stats.getEntityLoadCount());

我明白了:

29/10/2018 17.16.35:GMT:SummaryReportSection:writeToBothLogs:SEVERE: Report:295:2ndLevelPut:778
29/10/2018 17.16.35:GMT:SummaryReportSection:writeToBothLogs:SEVERE: Report:295:2ndLevelHit:0
29/10/2018 17.16.35:GMT:SummaryReportSection:writeToBothLogs:SEVERE: Report:295:2ndLevelMiss:0
29/10/2018 17.16.35:GMT:SummaryReportSection:writeToBothLogs:SEVERE: Report:295:com.jthink.songlayer.Song:2ndLevelPut:778
29/10/2018 17.16.35:GMT:SummaryReportSection:writeToBothLogs:SEVERE: Report:295:com.jthink.songlayer.Song:2ndLevelHit:0
29/10/2018 17.16.35:GMT:SummaryReportSection:writeToBothLogs:SEVERE: Report:295:com.jthink.songlayer.Song:2ndLevelMiss:0
29/10/2018 17.16.35:GMT:SummaryReportSection:writeToBothLogs:SEVERE: Report:295:com.jthink.songlayer.Song:2ndLevelSizeInMemory:1672432
29/10/2018 17.16.35:GMT:SummaryReportSection:writeToBothLogs:SEVERE: CacheName:com.jthink.songlayer.Song:Put:1333:Hit:2088:Miss:223:Size:223

所以Hibernate没有显示命中,但是CacheManager确实显示了点击率,这个差异的原因是什么?

BTW我的ehcache.xml文件是

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <defaultCache
            maxEntriesLocalHeap="500"
            eternal="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
    >
    <persistence strategy="localTempSwap"/>
    </defaultCache>
    <cache
            name="com.jthink.songlayer.Song"
            maxEntriesLocalHeap="5000"
            eternal="false"
            memoryStoreEvictionPolicy="LRU"
    >
    <persistence strategy="localTempSwap"/>
    </cache>
</ehcache>
答案

确保:

  • 查询缓存已启用: hibernate.cache.use_query_cache = true这个
  • 查询结果被缓存: query.setHint(“org.hibernate.cacheable”,“true”)

二级缓存将在插入/更新时更新。

如果禁用查询缓存或查询结果不可缓存,则在调用TypedQuery.getResultList()等操作时不会查询它。


至少这是Hibernate 5.3.6中的行为。只有在调用org.hibernate.event.internal.DefaultLoadEventListener.onLoad()时才会更新命中/未命中统计数据。如果其中任何一个属性为false,则在列出结果时永远不会触发此事件。

以上是关于为什么Hibernate二级静态显示与CacheManager不同的值的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate之二级缓存

Hibernate之二级缓存

hibernate之二级缓存

hibernate之二级缓存

(N)Hibernate 中的一级和二级缓存是啥?

mybatis 的二级缓存与延迟加载什么时候用