1

I configured my app to use query caching.

Hibernate config:

hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
hibernate.cache.use_query_cache=true

EHCache config:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false" monitoring="autodetect" dynamicConfig="false">
    <defaultCache
        maxEntriesLocalHeap="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        maxEntriesLocalDisk="10000000"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>
    <cache name="query.Dictionary.CountriesList"
        maxEntriesLocalHeap="10000" 
        maxEntriesLocalDisk="1000"
        eternal="false"
        timeToLiveSeconds="86400">
        <persistence strategy="localTempSwap" />
    </cache>
</ehcache>

DAO:

Criteria query = session.createCriteria(DictionaryCountry.class)
                .setCacheable(true)
                .setCacheRegion("query.Dictionary.CountriesList")
                .addOrder(Order.asc("name"));

Now when I try to populate a list of countries for the first time - the standard query is made (select * from ... where ... ). But when I do it for the second time - instead of getting from the cache the app executes a lot of get by id sql queries (select * from ... where id = ? ) ...

Is it normal behaviour?

Thank you

2 Answers 2

2

After a long time but today i had your same problem, here is an answer if someone needs help with ehcache+spring+hibernate

the previous configuration is correct, i only would add the next:

1.- Add: <property name="hibernate.cache.use_second_level_cache">true</property>

2.- Set in your models

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE )

3.- Add something like this to make your model cacheable

<cache name="com.yourcompany.yourproyect.model.DictionaryCountry
    maxEntriesLocalHeap="10000" 
    maxEntriesLocalDisk="1000"
    eternal="false"
    timeToLiveSeconds="86400">
    <persistence strategy="localTempSwap" />
</cache>

query.setCacheable(true); Required makes your models cacheable query.setCacheRegion("query.Dictionary.CountriesList"); optional as makes cacheable at query level

ehcache version:

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

I think that's it as this is working for me.

1
  • Thanks for reply! It seems that second level cache is necessary for query cache.. I can't configure the logging properly (it doesn't show any EHCache activity for putting-getting from-to cache) but at least I don't have these extra HQL-queries, so it seems that it works!
    – nKognito
    Jan 17, 2014 at 14:32
0

Yes second level cache is necessary for query cache. This is my configuration:

<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property><code>
<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</property>
<property name="hibernate.cache.use_structured_entries">true</property>
<property name="hibernate.generate_statistics">true</property>

And regarding to the multiple queries to database issue the real bug that I found was in here

With var1 = 1 and var2 = 20.

List<entity> objects = session.createCriteria(entity.class)  
                              .add(Restrictions.between("price",var1,var2))
                              .setCacheable(true)
                              .list();

The problem is: ehcache is not smart enough to retrieve from the cache if I perform a subsequent query with var1 = 21 and var2 = 40 since these objects have already been cached from the previous query.

I had the problem with dynamic parameter in one query so second level cache never was hit as the query always is different, it needs to have the same parameters.

Hope it help as this was my experience.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.