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.  [ 5 posts ] 
Author Message
 Post subject: Question on 2nd level cache and sessions
PostPosted: Wed May 31, 2006 9:10 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
I'm seeing the following unexpected behavior:

Class "Test" is declared with <cache usage="read-write" /> and 2nd level cache is enabled and query cache is enabled

1. Open session; load Test with ID 1 (NH issues SELECT statement, as expected); Close session
2. Open session; load Test with ID 1 (No database trip, as expected); Close session
3. Open session, load Test with ID 1 (No database trip, as expected); Change property of Test, Flush session (NH issues UPDATE statement, as expected); Close session
4. Open session; load Test with ID 1 (NH issues SELECT statement!); Close session

My question is why does NH issue another select statement in step 4? The updated object should still be in the level 2 cache, correct? I dont understand why issuing an UPDATE statement seems to evict the object from the cache. Any ideas appreciated. Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 9:48 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Afaik, changing a property like this updates the session cache correctly, and dirties the query and 2nd level caches. Seeing as you closed a session at the end of step 3, your session cache is gone, so the new session won't find any clean cached records, just dirtied ones, which it ignores. So I think the behaviour is correct.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 10:01 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
I suspect that you are right, the behavior is correct, but I'm not sure why the object is not updating the 2nd level cache instead of it just being dirtied. I'm sure there must be a reason but I don't totally understand it and I'm wondering if there is a way to get this behavior.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 10:55 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I believe that the 2nd level cache doesn't store java objects, instead it stores the scalars returned from a result set (meaning that the cache looks like a result set provider, just like a real DB). When you update an object and save it, Hibernate does not automatically select the corresponding row(s) back out of the DB. If it did, the 2nd level cache would be immediately updated, but it would be a huge performance hit. So instead, it just marks that cache entry as invalid, resulting in what you're seeing.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 4:49 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You have to use transactions for the second-level cache to be updated.


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