0
votes

i configured 2nd level hibernate cache with ehcache for some entities in my spring application.

The caches should live for 10 min, but the cached entities dont seem to live that long.

my entity looks like that:

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "eh_apples")
public class Apple {
...
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "eh_eaters")
    @ManyToMany(fetch = FetchType.EAGER)
    private Set<AppleEater> eaters = new HashSet<AppleEater>();
....
}

part of persistence.xml:

<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.use_minimal_puts" value="true"/>    
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
<property name="hibernate.cache.provider_configuration_file_resource_path" value="META-INF/ehcache.xml"/>
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"></property>

ehcache.xml:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" 
updateCheck="true"
monitoring="autodetect" 
dynamicConfig="true" 
maxBytesLocalHeap="500M">

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

<cache
    name="eh_apples" 
    eternal="false"
    overflowToDisk="false"
    memoryStoreEvictionPolicy="LRU" 
    timeToLiveSeconds="600">
    <persistence strategy="localTempSwap" />
</cache>

<cache
    name="eh_eaters" 
    eternal="false"
    overflowToDisk="false"
    memoryStoreEvictionPolicy="LRU" 
    timeToLiveSeconds="600">
    <persistence strategy="localTempSwap" />
</cache>

<cache 
    name="org.hibernate.cache.internal.StandardQueryCache"
    eternal="false" 
    timeToLiveSeconds="600">
    <persistence strategy="localTempSwap" />
</cache>

<cache 
    name="org.hibernate.cache.spi.UpdateTimestampsCache"
    eternal="true">
    <persistence strategy="localTempSwap" />
</cache>

</ehcache>

on localhost:

When retrieving 500 apples from DB for the first time, it takes around 1000ms. The log shows that they are put in memory. Then for some of the following requests, it doesnt hit the DB anymore but read them from memory.

After less than 10 min, it starts to hit the DB again for the same entities, as shown here:

time:09:37:44.143 duration : 903
time:09:37:53.295 duration : 92
time:09:37:58.278 duration : 67
time:09:38:57.701 duration : 61
time:09:39:25.384 duration : 55
time:09:40:10.049 duration : 1185
time:09:44:21.507 duration : 1005
time:09:44:24.802 duration : 99

I wonder why the caches dont live up to 10 min. Maybe i missed some essential part while reading ehcache docs or there is some issue with my setup.

I appreciate any hint or help, pls dont hate <3

edit: run statistics

when i ran statistics i got following warning:

2017-03-04 10:45:54,563 [tomcat-http--90] WARN  net.sf.ehcache.config.ConfigurationFactory - No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: jar:file:/C:/repo/sts-bundle/pivotal-tc-server-developer-3.2.2.RELEASE/base-instance/wtpwebapps/Apples/WEB-INF/lib/ehcache-2.10.2.2.21.jar!/ehcache-failsafe.xml
2
Do you have <shared-cache-mode>ALL</shared-cache-mode> in persistence.xml??Akshay
no, i read about this option - but i did not understand 100% what it is good for.user1386375
as far as i understood <shared-cache-mode>ALL</shared-cache-mode> will just make all my entities cacheable. but dont i have cached entities already? they are just not cached as long as i want them to :/user1386375
yeah..which server you are using?Akshay
locally on windows: pivotal tc server v3.2 or Tomcat v8.0user1386375

2 Answers

1
votes

My first guess would be that you reach maxEntriesLocalHeap or maxBytesLocalHeap which than causes eviction.

You can confirm that by looking at the statistics.

0
votes

The problem was: net.sf.ehcache.config.ConfigurationFactory - No configuration found.

Configuring ehcache from ehcache-failsafe.xml

The solution was, moving ehcache from META-INF to src/main/resources:

<property name="hibernate.cache.provider_configuration_file_resource_path" 
          value="classpath:ehcache.xml"/>