-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Second Level caching issues with HQL
PostPosted: Mon May 28, 2007 11:28 am 
Newbie

Joined: Mon May 28, 2007 10:37 am
Posts: 3
Hi All,

After much googling and combing thru the Hibernate forum, I seek your help on following issue:

Context:
I am trying to set-up L2 caching mechanism using EhCache as the cache provider on Hibernate3.2. In order to do so, I have the done the following configuration:

a) Enabled caching in hibernate configuration file:
<property name="cache.use_second_level_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

b) Tagged relevant hbm.xml files with <cache> tag

<cache usage="read-only"/>

c) Configured default cache regions in ehcache.xml

<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/>
</ehcache>


Issue:

Now when I tried to use HQL to load objects, the objects though getting cached in L2 are not getting fetched from L2 on subsequent hits. I enabled the Second-Level Statistics(pasted below) to decipher the same

First Hit using HQL:
Statistics[start time=1180365059200,sessions opened=1,sessions closed=0,transactions=0,successful transactions=0,optimistic lock failures=0,flushes=0,connections obtained=38,statements prepared=38,statements closed=38,second level cache puts=9,second level cache hits=0,second level cache misses=0,entities loaded=64,entities updated=0,entities inserted=0,entities deleted=0,entities fetched=27,collections loaded=9,collections updated=0,collections removed=0,collections recreated=0,collections fetched=9,queries executed to database=2,query cache puts=1,query cache hits=0,query cache misses=1,max query time=265]

Second Hit using HQL:
Statistics[start time=1180365059200,sessions opened=2,sessions closed=1,transactions=0,successful transactions=0,optimistic lock failures=0,flushes=0,connections obtained=76,statements prepared=76,statements closed=76,second level cache puts=18,second level cache hits=0,second level cache misses=0,entities loaded=128,entities updated=0,entities inserted=0,entities deleted=0,entities fetched=54,collections loaded=18,collections updated=0,collections removed=0,collections recreated=0,collections fetched=18,queries executed to database=4,query cache puts=2,query cache hits=0,query cache misses=2,max query time=265]

As you can see that on subsequent hits second-level-cache puts doubles-up. Also, I have tried enabling the query cache in the hope of fixing this but to no avail.

However, caching works perfectly if I use session.load() or session.get() instead of HQL.

Any pointers in this regard would be highly appreciated!

Thanks in Advance
-K


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 28, 2007 8:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Using HQL falls to the query cache to resolve. The query cache is matched using query and args to make the cache key. If matched it uses the stored ids against the query cache key. This then falls to the second level cache to collect the object data sets based on ID collection. Obviously, this all depends on what happens around this code as something might invalidate the various data caches. In simple testing environments the issue is often a simple problem. Having said that you did tell the HQL query operation to use the query cache?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 2:33 am 
Newbie

Joined: Mon May 28, 2007 10:37 am
Posts: 3
Thanks David for the direction. It did help me to look at the problem from invalidation perspective. The issue lay with ehcache configuration that was, I guess, invalidating the objects.

The configuration that I was using was:
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/>
</ehcache>

Per the ehcache documentation:

Quote:
eternal
protected boolean eternal
Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired.

timeToIdleSeconds
protected int timeToIdleSeconds
the time to idle for an element before it expires. Is only used if the element is not eternal.A value of 0 means do not check for idling.

timeToLiveSeconds
protected int timeToLiveSeconds
Sets the time to idle for an element before it expires. Is only used if the element is not eternal. This attribute is optional in the configuration. A value of 0 means do not check time to live.


With eternal set as false, and timeToIdleSeconds & timeToLiveSeconds as 0, we should have had the objects cached in L2 for eternity but it did not work for me.

However, by setting eternal as true, I was able to ignore timeouts and cache objects.

This bit of eh-cache documentation is little ambiguous which can do with little overhauling.

Thanks,
Kunal

P.S: I tried loading (using get()) a basic pojo with no relations (nothing), and I could observe the similar behaviour with it as well.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 30, 2007 9:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Glad you found it. I did not look closely at your ehcache settings. Good work.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.