-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Why the hibernate second level cache is not working??
PostPosted: Tue Jan 29, 2008 3:05 am 
Newbie

Joined: Tue Jan 29, 2008 2:56 am
Posts: 4
I am using spring-hibernate for my DAO layer. I have configured a second level ehcache for the hibernate. The problem is when I hit the database and bring the records they are inserted into cache, but when i again execute the same method, the hibernate is not picking data from cache instead it is going to database.
I have enabled the cache statistics ,I can see the cache put count but the hit count always remains zero. I am using criteria interface for executing the queries. Any help will be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 3:38 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
The second level cache holds only enities and is used with Session.get.

If you want query results cached you need to configure a query cache. See 19.4 in http://www.hibernate.org/hib_docs/refer ... mance.html

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 5:18 am 
Newbie

Joined: Tue Jan 22, 2008 5:40 am
Posts: 10
Location: Hungary
E.g. in the hibernate config file:

Code:
<hibernate-configuration>
  <session-factory>
  ...
    <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
  ...
  </session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 8:39 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
That would be the 'normal' 2nd level Cache. According to the reference manual you need in addition to that:

Code:
<hibernate-configuration>
  <session-factory>
  ...
    <property name="hibernate.cache.use_query_cache true">true</property>
  ...
  </session-factory>
</hibernate-configuration>


I never tried it yet, just reading the documentation ...

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 10:42 am 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
A few tips:

1) You can cache instances of nominated classes. This cache will then be used to get objects of those classes by identifier, or when hibernate automatically wires foreign keys.

2) When you run a query via HQL or the Criteria API, the results will NOT come from the cache. If you use a query cache, it only stores the identifier of each result of the query, so it makes sense to nominate the class of the result objects as cachable if you want decent performance.

3) You can cache OneToMany, ManyToMany collections, but again only identifiers are stored so the same principle applies - nominate the class T in Collection<T> as cachable!

4) When you are testing your caching strategy, make sure you turn on hibernate sql logging (log4j.logger.org.hibernate.SQL=DEBUG)



Here's a good link that helped me learn how the 2nd level cache works:

http://www.javalobby.org/java/forums/t48846.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 11:49 am 
Newbie

Joined: Tue Jan 22, 2008 5:40 am
Posts: 10
Location: Hungary
Ok, this is right. The second level cache by queries is used to store some information from the query. (The hibernate make some transformations with the hql queries to obtain the sql query and the query logic, this transformation will be faster with query cache.) This modifications in the hibernate config file results the use of the query cache:

Code:
<hibernate-configuration>
  <session-factory>
  ...
    <property name="hibernate.cache.use_query_cache">true</property>
    <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
  ...
  </session-factory>
</hibernate-configuration>


When you use big loops with hql queries inside, it is a good decision to use the query cache, the program can be 50% faster. (In this case avoid the inline values in the hql queries, use parameters instead.)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 2:41 pm 
Newbie

Joined: Tue Jan 29, 2008 2:56 am
Posts: 4
In case of using query cache, I think hibernate will use the query and its parameters to decide which object to pick from cache....correct me if wrong!
but I want object to be picked from cache just depending on its identity.
that is irrespective of the query being executed I want the object to be picked from cache if it is already there.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 4:52 pm 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
lostinhib wrote:
In case of using query cache, I think hibernate will use the query and its parameters to decide which object to pick from cache....correct me if wrong!
but I want object to be picked from cache just depending on its identity.
that is irrespective of the query being executed I want the object to be picked from cache if it is already there.


If you run a query that's not been cached, the results will not be loaded from the cache, even if they are actually present in the 2nd level cache.

However, once that query has been run once, subsequent uses of the same query will fetch the results exclusively from the cache (if you cached the query).

Hope that helps.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 30, 2008 3:58 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
lostinhib wrote:
In case of using query cache, I think hibernate will use the query and its parameters to decide which object to pick from cache....correct me if wrong!
but I want object to be picked from cache just depending on its identity.
that is irrespective of the query being executed I want the object to be picked from cache if it is already there.


If you really want to force it you could select just the id's and then get the individual objects by id. If present they will be created from the cache. if not you'll be causing lots of sql ...

Why do you want the stuff to come from the cache? It would force another roundtrip to the database for cache misses as described above.

Jens

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 1:25 am 
Newbie

Joined: Tue Jan 29, 2008 2:56 am
Posts: 4
If you really want to force it you could select just the id's and then get the individual objects by id. If present they will be created from the cache. if not you'll be causing lots of sql ...

Why do you want the stuff to come from the cache? It would force another roundtrip to the database for cache misses as described above.

Jens[/quote]

In my application database has some static data, which I want to cache at first load..
suppose I execute at first the query
select * from static_table
the query cache will add the records fetched into cache..
later on I execute the query
select * from static_table where [constraint1]
this query will again go to database..which I dont want...coz all records from static_table are already there in cache.

So by using second level cache whether it is possible to do what I want?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 2:43 am 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
Quote:

In my application database has some static data, which I want to cache at first load..
suppose I execute at first the query
select * from static_table
the query cache will add the records fetched into cache..
later on I execute the query
select * from static_table where [constraint1]
this query will again go to database..which I dont want...coz all records from static_table are already there in cache.

So by using second level cache whether it is possible to do what I want?


After you have cached everything, run your queries as a Projection on id, then look up each record returned from the cache. Remember that constraints rely soley on the database functionality so you can't avoid going to the database.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 9:11 am 
Newbie

Joined: Tue Jan 29, 2008 2:56 am
Posts: 4
It looks better for me to just configure the hibernate query cache and left all to the hibernate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 04, 2008 7:30 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
lostinhib wrote:
If you really want to force it you could select just the id's and then get the individual objects by id. If present they will be created from the cache. if not you'll be causing lots of sql ...

Why do you want the stuff to come from the cache? It would force another roundtrip to the database for cache misses as described above.

Jens


In my application database has some static data, which I want to cache at first load..
suppose I execute at first the query
select * from static_table
the query cache will add the records fetched into cache..
later on I execute the query
select * from static_table where [constraint1]
this query will again go to database..which I dont want...coz all records from static_table are already there in cache.

So by using second level cache whether it is possible to do what I want?[/quote]

No your second level cache won't do that for you. But it is possible to run queries (HQL or Criteria I think but not SQL) against a collection. So you could load everything with a select * query and then query against the resulting collection.

Jens

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject: just to make sure that i've understood this correct
PostPosted: Mon Feb 04, 2008 12:55 pm 
Newbie

Joined: Mon Feb 04, 2008 10:09 am
Posts: 3
Hi, i have basically the same problem as lostinhib, and i think the answer is in already posted, but since i am rather new at this i would like to be sure that i got it right, so will someone please confirm that following?

I get a list of objects from the DB with hibernatetemplate.loadAll this list is saved in the 2nd-lvl-cache (as strings references by the id, am i correct?).

Then i do exactly the same thing again, at first i would think that this caused hibernate to get the entire list from the cache. (does it) ?

Now getting the two lists again but in between changing some value in one of the db rows. (here i once again expected the two list to be the same, but for some reason the change i did in the db is there instead of the one from the cache (which is actually a good think, besides making me unsure if that 2nd list comes from the cache or from the db)

presuming that the 2nd list does come from the cache, why does hibernate refresh the changed object before it's time to live has run out?

until now i haven't used query cache, what impact on my lists would this have? if any at all?

if i changed my loadall method to some query like ("from sometable") i should get the same list of object, but would this get med the list from the cache the 2nd time?

Hmm, i have just realized that this post turned out to be a lot of questions, sorry about that, but im still hoping that someone might have the time and knowledge to answer.

/ Anders


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 04, 2008 1:10 pm 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
Anders, this link is a pretty good explanation of the 2nd level cache:

http://www.javalobby.org/java/forums/t48846.html

Quote:
I get a list of objects from the DB with hibernatetemplate.loadAll this list is saved in the 2nd-lvl-cache (as strings references by the id, am i correct?).


All results will be individually cached if a cache region is configured for the resulting class.


Quote:
Then i do exactly the same thing again, at first i would think that this caused hibernate to get the entire list from the cache. (does it) ?


No, this won't look in the cache. Behind the scenes, the HibernateTemplate builds a query to return all data. Query results ALWAYS come from the db unless the query is cached.

Quote:
presuming that the 2nd list does come from the cache, why does hibernate refresh the changed object before it's time to live has run out?


It doesn't, for reasons mentioned above.

Quote:
until now i haven't used query cache, what impact on my lists would this have? if any at all?


I don't think you can use the query cache with hibernateTemplate.loadAll(), but you can just as easily build your own criteria to return all data and set cachable to true.


Quote:
if i changed my loadall method to some query like ("from sometable") i should get the same list of object, but would this get med the list from the cache the 2nd time?


Yes! As long as you have configured the query to be cachable.

-Paul


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.