-->
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.  [ 6 posts ] 
Author Message
 Post subject: hibernate 2nd level cache issue
PostPosted: Fri Feb 08, 2008 12:25 pm 
Newbie

Joined: Thu Jan 31, 2008 1:48 pm
Posts: 11
it returns the query result from the cache the 2nd time but it seem to be clearing the cache. It issues the db call the 3rd time and again caches the result so the 4th time it would return the result from the cache. So it returns the result from the cache alternate time. And every other time it issues query to db. Does anybody know what am I missing here? I have the query cache enabled in hibernate configuration file and the object's hibernate mapping file.

I'm using hibernate 3.1.3. I have read-only cache mode configured for the class in question.

I have following hibernate configuration ...
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>


Here is my code....I would appreciate any help on this. Thanks in advance!


public List findAllCached() throws DataAccessException {
List dObjList = new ArrayList();
String query = "from " + domainObjectClass.getName();
try {
if (getSession() != null) {
Query qryObj = getSession().createQuery(query).setCacheable(true);
if (qryObj != null)
dObjList = qryObj.list();
}
}
catch (HibernateException e) {
logger.error(ExceptionUtils.getStackTrace(e));
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
return dObjList;
}

domainObjectClass is correctly being set with the class name prior to calling the above method.

Thanks a lot in advance!


Top
 Profile  
 
 Post subject: Re: hibernate 2nd level cache issue
PostPosted: Fri Feb 08, 2008 1:00 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
There are two things. First you have to define a cache region for this query and configure that cache region in your cache provider to be eternal or something with a long life span. Second if hibernate detects an update in any entity involved in that query, it will throw away the results for that query so a database hit is an expected behaviour.



Farzad-


Top
 Profile  
 
 Post subject: Re: hibernate 2nd level cache issue
PostPosted: Fri Feb 08, 2008 4:27 pm 
Newbie

Joined: Thu Jan 31, 2008 1:48 pm
Posts: 11
farzad wrote:
There are two things. First you have to define a cache region for this query and configure that cache region in your cache provider to be eternal or something with a long life span. Second if hibernate detects an update in any entity involved in that query, it will throw away the results for that query so a database hit is an expected behaviour.



Farzad-


Thanks for your quick reply. I changed eternal from false to true in the following ehcache.xml configuration and it worked.The data in the table never changes, i'm wondering why hibernate discard the query cache?

<cache name="com.MyClass"
maxElementsInMemory="100"
eternal="true"
timeToIdleSeconds="1800"
timeToLiveSeconds="36000"
overflowToDisk="false"
/>

I read in Hibernate in Action book that there are three types of cache region : default, named and timestamp. It further says that the query cache result is stale or not is decided by timpestamp cache region. I did not find any example how to disable/enable/configure timestamp cache (UpdateTimeStampCache is the class). Do you have any example configuration for that? Also what is the difference between timeToIdleSeconds and timeToLiveSeconds ? With eternal=false and timeToIdleSeconds=1800, should it not cache the query for 30 minutes at least?


Top
 Profile  
 
 Post subject: Re: hibernate 2nd level cache issue
PostPosted: Fri Feb 08, 2008 4:36 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
srkamani wrote:
Thanks for your quick reply. I changed eternal from false to true and it worked.The data in the table never changes, i'm wondering why hibernate discard the query cache?

<cache name="com.MyClass"
maxElementsInMemory="100"
eternal="true"
timeToIdleSeconds="1800"
timeToLiveSeconds="36000"
overflowToDisk="false"
/>

I read in Hibernate in Action book that there are three types of cache region : default, named and timestamp. It further says that the query cache result is stale or not is decided by timpestamp cache region. I did not find any example how to disable/enable/configure timestamp cache (UpdateTimeStampCache is the class). Do you have any example configuration for that? Also what is the difference between timeToIdleSeconds and timeToLiveSeconds ? With eternal=false and timeToIdleSeconds=1800, should it not cache the query for 30 minutes at least?


You have mixed the two a little bit. Each cached query is stored in a region and it is the underlying cache technology's configuration to decide how long that stays in cache. Hibernate does not get involves in this detail. However, Hibernate does care if the cache query is good. The criteria here is that you can't rely on a query result if you know one of the table has had modifications. For this reason, hibernate uses a timestamp cache in which it knows when a query has been fetched last and when the involving entities have been updated last. If a query is using Person entity, the query is cached 20 minutes ago and Person has been updates 10 minutes ago then hibernate throws away the cached query. It's pure logic. Timestamp cache is also a region. If you look at ehcache logs you will notice it tells you that you have not configured any region for timestamp cache. There you can find the name or it should be in the HB's documentation.


The eternal setting in ehcache makes the other time settings redundant. If false, timeToLive is a total time an entry stays in a cache region. timeToIdle specifies how long it should stay in cache if it is not referenced. These two values work together. However, a timeToLive < timeToIdle setting doesn't make much sense.


Farzad-


Top
 Profile  
 
 Post subject: Re: hibernate 2nd level cache issue
PostPosted: Fri Feb 08, 2008 6:20 pm 
Newbie

Joined: Thu Jan 31, 2008 1:48 pm
Posts: 11
farzad wrote:
srkamani wrote:
Thanks for your quick reply. I changed eternal from false to true and it worked.The data in the table never changes, i'm wondering why hibernate discard the query cache?

<cache name="com.MyClass"
maxElementsInMemory="100"
eternal="true"
timeToIdleSeconds="1800"
timeToLiveSeconds="36000"
overflowToDisk="false"
/>

I read in Hibernate in Action book that there are three types of cache region : default, named and timestamp. It further says that the query cache result is stale or not is decided by timpestamp cache region. I did not find any example how to disable/enable/configure timestamp cache (UpdateTimeStampCache is the class). Do you have any example configuration for that? Also what is the difference between timeToIdleSeconds and timeToLiveSeconds ? With eternal=false and timeToIdleSeconds=1800, should it not cache the query for 30 minutes at least?


You have mixed the two a little bit. Each cached query is stored in a region and it is the underlying cache technology's configuration to decide how long that stays in cache. Hibernate does not get involves in this detail. However, Hibernate does care if the cache query is good. The criteria here is that you can't rely on a query result if you know one of the table has had modifications. For this reason, hibernate uses a timestamp cache in which it knows when a query has been fetched last and when the involving entities have been updated last. If a query is using Person entity, the query is cached 20 minutes ago and Person has been updates 10 minutes ago then hibernate throws away the cached query. It's pure logic. Timestamp cache is also a region. If you look at ehcache logs you will notice it tells you that you have not configured any region for timestamp cache. There you can find the name or it should be in the HB's documentation.


The eternal setting in ehcache makes the other time settings redundant. If false, timeToLive is a total time an entry stays in a cache region. timeToIdle specifies how long it should stay in cache if it is not referenced. These two values work together. However, a timeToLive < timeToIdle setting doesn't make much sense.


Farzad-


Farzad,
Thanks for you reply again.
Could you please provide me following two, if you can.
1. what is the hibernate property name for timestamp configuration(i could not find it)
2 how do you configure cache region that you define into the cache provider. I'm using hibernate's, org.hibernate.cache.EhCacheProvider.

Thanks


Top
 Profile  
 
 Post subject: Re: hibernate 2nd level cache issue
PostPosted: Fri Feb 08, 2008 6:42 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
1- If no cache region prefix is specified then it is: net.sf.hibernate.cache.UpdateTimestampsCache


2- http://ehcache.sourceforge.net/documentation/configuration.html, look at ehcache.xml



Farzad-


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.