Why does Hibernate 2nd level statics show different values then CacheManager
Asked Answered
T

1

7

Using Hibernate 4.3.11 and Ehcache 2.7.0

Why does the hits/Misses returned by Hibernates getStatistics() not matched values returned by CacheManager. I have enabled 2nd level caching for a single class.

So with this code

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());

I get:

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

So Hibernate shows no hits, but CacheManager does show hits, what accounts for this difference ?

BTW my ehcache.xml file is

<?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>
Transpontine answered 29/10, 2018 at 17:24 Comment(0)
S
0

Make sure:

  • the query cache is enabled:

    hibernate.cache.use_query_cache=true

  • query results are cached:

    query.setHint( "org.hibernate.cacheable", "true")

The second level cache will be updated on inserts/updates.

If the query cache is disabled or the results of a query aren't cacheable, it won't be consulted when invoking operations like TypedQuery.getResultList().


At least this is the behaviour in Hibernate 5.3.6. Hit/miss stats are only updated when org.hibernate.event.internal.DefaultLoadEventListener.onLoad() is called. If either of these properties are false, this event is never fired when listing results.

Shoe answered 8/11, 2018 at 22:58 Comment(1)
Okay so I dont have query cache enabled but you havent really explained why statsare different with it not enabled.Transpontine

© 2022 - 2024 — McMap. All rights reserved.