Почему статика Hibernate 2-го уровня показывает другие значения, чем CacheManager - PullRequest
0 голосов
/ 29 октября 2018

Использование Hibernate 4.3.11 и Ehcache 2.7.0

Почему хиты / пропуски, возвращаемые Hibernates, getStatistics () не соответствуют значениям, возвращаемым CacheManager. Я включил кэширование 2-го уровня для одного класса.

То есть с этим кодом

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 показывает хиты, что объясняет эту разницу?

Кстати, мой файл 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>

1 Ответ

0 голосов
/ 09 ноября 2018

Убедитесь:

  • кеш запросов включен:

    hibernate.cache.use_query_cache = истина

  • результаты запроса кэшируются:

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

Кэш второго уровня будет обновляться при вставках / обновлениях.

Если кеш запросов отключен или результаты запроса не кэшируются, с ним не будут обращаться при вызове таких операций, как TypedQuery.getResultList ().


По крайней мере, это поведение в Hibernate 5.3.6. Статистика попаданий / промахов обновляется только при вызове org.hibernate.event.internal.DefaultLoadEventListener.onLoad () . Если любое из этих свойств имеет значение false, это событие никогда не вызывается при выводе результатов.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...