File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Hibernate Query Cache Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Query Cache" Watch "Hibernate Query Cache" New topic

Hibernate Query Cache

ying lam
Ranch Hand

Joined: May 17, 2004
Posts: 85

I try to setup Hibernate Query Cache, I am using ehcache.

It works in the beginning (i.e. if i execute the same query multiple times,
it does not send the sql commands to mysql everytime).

however, after about 1 minutes, the cache seems expire (i.e. I see hibernate executes sql commmands to the database.

I have the following configuration in my ehcache.xml file. So the query cache should last 2 hours (7200 seconds), I don't understand why my query cache expires so soon.

Thank you for any help.

<defaultCache maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="7200" timeToLiveSeconds="7200" diskPersistent="false" diskExpiryThreadIntervalSeconds="7200"/>
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
- show the code where you create the query
- are there more different queries than the cache size ?

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

If we are talking about the Query Cache, do the parameters of your query change? This will mean the cache needs rebuilt.

However, you mention ehcache, which suggests this might be about Second Level caching, which is nothing to do with Query Caching. In which case, you might want to check that your cache has not become full.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
ying lam
Ranch Hand

Joined: May 17, 2004
Posts: 85
Thanks for the help.

No, I am pretty sure that the query only return 1 result.
And I am sure that the query parameters are the same everything i execute the query because I just reloading the same page again and again (which trigger the query with parameters).

I appreciate if anyone can give me some ideas to my problem.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1427
you need to set the hibernate.cache.use_query_cache property in the hibernate.cfg.xml file to true, as follows:

<property name="hibernate.cache.use_query_cache">true</property>

Then, you use the setCacheable() method. refer this link.

QueryCaching is diffferent from the second level caching.
I agree. Here's the link:
subject: Hibernate Query Cache
It's not a secret anymore!