1
votes

I'm trying, in my Spring(3.2)+Hibernate(4.2) application, to make query_cache never expire

I tried the following configurations, the cache is working but after 120 seconds, my cacheable query hit the database, even if timeToIdleSeconds and timeToLiveSeconds are set to a value greater then 120

<?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="false">  

 <defaultCache
    maxElementsInMemory="10000"
    eternal="false"
    timeToIdleSeconds="1800"
    timeToLiveSeconds="3600"
    overflowToDisk="true"
    maxElementsOnDisk="10000000"
    diskPersistent="false"
    diskExpiryThreadIntervalSeconds="1800"  />

<cache name="org.hibernate.cache.StandardQueryCache"
    maxEntriesLocalHeap="25"
    eternal="false"
    timeToIdleSeconds="1800"        
    timeToLiveSeconds="3600">
        <persistence strategy="localTempSwap"/>
</cache>

<cache name="org.hibernate.cache.UpdateTimestampsCache"
    maxEntriesLocalHeap="5000"
    timeToIdleSeconds="1800"
    timeToLiveSeconds="3600"
    eternal="false">
        <persistence strategy="localTempSwap" />
</cache>
</ehcache>

I included Ehcache using:

    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.6.6</version>
    </dependency>        

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>4.2.6.Final</version>
    </dependency>     

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.1</version>
    </dependency> 

Note: I have already tried with: timeToIdleSeconds="0" and timeToLiveSeconds="0" but with no luck, I get the same behavior, 120 seconds and the cache get cleared. Here is the full log:

21:52:11,128 DEBUG StandardQueryCache:131 - Checking cached query results in region: org.hibernate.cache.internal.StandardQueryCache

21:52:11,129 DEBUG EhcacheGeneralDataRegion:69 - key: sql: select this_.townID as townID1_33_1_, this_.name as name2_33_1_, this_.zip_code as code_pos3_33_1_, this_.regionID as regionID4_33_1_, region2_.regionId as regionId1_27_0_, region2_.name as name2_27_0_ from town this_ left outer join region region2_ on this_.regionID=region2_.regionId; parameters: ; transformer: org.hibernate.transform.CacheableResultTransformer@13a525

21:52:11,129 DEBUG EhcacheGeneralDataRegion:76 - Element for key sql: select this_.townID as townID1_33_1_, this_.name as name2_33_1_, this_.zip_code as code_pos3_33_1_, this_.regionID as regionID4_33_1_, region2_.regionId as regionId1_27_0_, region2_.name as name2_27_0_ from town this_ left outer join region region2_ on this_.regionID=region2_.regionId; parameters: ; transformer: org.hibernate.transform.CacheableResultTransformer@13a525 is null

21:52:11,129 DEBUG StandardQueryCache:137 - Query results were not found in cache

Hibernate: select this_.townID as townID1_33_1_, this_.name as name2_33_1_, this_.zip_code as code_pos3_33_1_, this_.regionID as regionID4_33_1_, region2_.regionId as regionId1_27_0_, region2_.name as name2_27_0_ from town this_ left outer join region region2_ on this_.regionID=region2_.regionId

21:52:11,137 DEBUG StandardQueryCache:104 - Caching query results in region: org.hibernate.cache.internal.StandardQueryCache; timestamp=5657678361137154

21:52:11,138 DEBUG EhcacheGeneralDataRegion:100 - key: sql: select this_.townID as townID1_33_1_, this_.name as name2_33_1_, this_.zip_code as code_pos3_33_1_, this_.regionID as regionID4_33_1_, region2_.regionId as regionId1_27_0_, region2_.name as name2_27_0_ from town this_ left outer join region region2_ on this_.regionID=region2_.regionId; parameters: ; transformer: org.hibernate.transform.CacheableResultTransformer@13a525 value: [5657678361137154, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

I activated the 2nd level cache and the query_cache using:

<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop> 
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>                
<prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>

EDIT:

My query is generated using the Criteria API:

public List<Town> readAll() {
  Criteria crit = getCurrentSession().createCriteria(Town.class);
  crit.setCacheable(true);
  return crit.list();
}

I'm using read-write as CacheConcurrencyStrategy. So I My entity Town I have this:

<cache usage="read-write" />

EDIT: I just saw this when starting my application:

22:10:45,570  WARN ConfigurationFactory:136 - No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%207.0/work/Catalina/localhost/projet/loader/ehcache-failsafe.xml
22:00:49,812  INFO UpdateTimestampsCache:61 - HHH000250: Starting update timestamps cache at region: org.hibernate.cache.spi.UpdateTimestampsCache
22:00:49,818  WARN AbstractEhcacheRegionFactory:180 - HHH020003: Could not find a specific ehcache configuration for cache named [org.hibernate.cache.spi.UpdateTimestampsCache]; using defaults.

I think that the configurations I made in WEB-INF/ehcache.xml are not considered what's wrong? is it the wrong place?

2
Do you write to the tables that the query queries? Are you sure the query is exactly the same as the previous one, with the exact same parameters?JB Nizet
@JBNizet I updated my questionHidalgo
My test scenario is simple: I trigger my JSF page where I display the list of towns, the query is executed, so the the entities are putted in the cache, after 1 minutes, I refresh the page, I see no select query in my log (cache is working), and I wait more then 2 minutes, I refresh again and I get: Query results were not found in cache. I will update my question with more log.Hidalgo
And have you also increased the timeout of the UpdateTimestamps cache?JB Nizet
@JBNizet you mean timeToIdleSeconds and timeToLiveSeconds of UpdateTimestampsCache? if yes, look at ehcache.xml, I set them to 1800 and 3600Hidalgo

2 Answers

5
votes

the main problem was that the ehcache.xml wasn't found by AbstractEhcacheRegionFactory, so I folowed these steps:

1) I removed:

<prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>

and I put ehcache.xml in src/main/java, it's default location that I found in the documentation

2) I replaced this:

org.hibernate.cache.StandardQueryCache 

by

org.hibernate.cache.internal.StandardQueryCache

AND this

org.hibernate.cache.UpdateTimestampsCache

by

org.hibernate.cache.spi.UpdateTimestampsCache

3) I set timeToIdleSeconds and timeToLiveSeconds of UpdateTimestampsCache to 0

1
votes

I did what you suggested, but I did not want to move the ehcache.xml in the sources directory, so instead I used this:

<prop key="hibernate.cache.provider_configuration_file_resource_path">classpath:ehcache.xml</prop>

And it worked just fine. (the ehcache.xml was in the resources directory)