Я использую Spring Data JPA, Hibernate 4.3.10.Final, Ehcache на Wildfly 8.2. Я хочу проверить, как сервер приложений обрабатывает все мои данные в кеше.Включить Ehcache для всех объектов
Вопрос: Можно ли включить кеш второго уровня для всех отсканированных объектов по умолчанию?
На самом деле, я хочу, чтобы не добавлять @Cache
на всех объектах (так как проект имеет 100+).
JPA свойства
... data source definition ...
<property name="sharedCacheMode" value="ALL" />
Hibernate свойства
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="net.sf.ehcache.configurationResourceName">/ehcache.xml</prop>
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
dynamicConfig="true">
<diskStore path="java.io.tmpdir/ehcache" />
<defaultCache maxElementsInMemory="1000000000"
eternal="true"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
statistics="true"
>
</defaultCache>
<!--
<cache name="org.hibernate.cache.internal.StandardQueryCache"
eternal="false"
timeToLiveSeconds="120">
<persistence strategy="localTempSwap"/>
</cache> -->
</ehcache>
EDIT: Пробовал <prop key="javax.persistence.sharedCache.mode">ALL</prop>
, как предложил Драган Бозанович, но это не влияет на процесс.
Вот статистика логарифм числа SELECT e FROM MyEntity e
выполненных в 2 раза:
17:00:00,597 | INFO | StatisticalLoggingSessionE:275 | | Session Metrics {
71157 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
145393 nanoseconds spent preparing 1 JDBC statements;
386233 nanoseconds spent executing 1 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
822075 nanoseconds spent performing 41 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}
17:00:00,597 | INFO | StatisticalLoggingSessionE:275 | | Session Metrics {
63973 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
127262 nanoseconds spent preparing 1 JDBC statements;
282918 nanoseconds spent executing 1 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
452598 nanoseconds spent performing 41 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}
Там в 41 возвращаемых строк, это правильно. Но я вижу 41 L2C puts
2 раза, не должно быть 41 L2C puts
, тогда 41 L2C hits
?
Кажется, что конфигурация вступила в силу - вы храните объекты в кеш-памяти L2. Что касается статистики, попробуйте прочитать объект по id ('entityManager.find (MyEntity.class, id)' или что-то подобное в Spring Data). Здесь вы выполняете запрос, а Hibernate должен выполнить его в базе данных. –
Работает лучше с '.setHint (" org.hibernate.cacheable ", true)' :). Спасибо за помощь ! –
Вы добавили этот намек на запрос? Если да, действительно ли вы хотите, чтобы запрос был кэшируемым? И почему это работает, поскольку вы установили 'use_query_cache' в' false'? –