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: Caching and Editing DB Directly
PostPosted: Mon Feb 04, 2008 1:02 pm 
Newbie

Joined: Mon Jan 07, 2008 8:26 pm
Posts: 7
Hi all,

My client is editing values in the database directly and not seeing the changes show up; I think this is because NHibernate is caching the old values. What's the recommended best-practise in this situation, and how can I clear the cache easily?


Top
 Profile  
 
 Post subject: Caching and Editing DB Directly
PostPosted: Mon Feb 04, 2008 2:17 pm 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

If you mean NHibernate is holding a copy in the session's first-level cache, then simply calling ISession.Clear() should do the trick; then re-load your objects.

You might want to call ISession.Flush() first to flush out any changes you've made using NHibernate first.


Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 4:04 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
The problem is, how do you do this abstractly? I.e. how can you find out what it actually in the first level session cache in order to reload it? I guess you can do a flush and implement IInterceptor.PreFlush() to remember the list of what's in the cache, since that appears to be the only place where the 1st level cache contents are exposed. Then after the flush, get that list from your implementation of IInterceptor and reload everything by its ID. But that's kind of lame way to get the cache contents ...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 4:29 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
ramsay,

Although updating the database by means other than Hibernate is not recommended, a query.list() should give you the freshest data, no matter what, because it always executes a query to the database.
Caching only works with session.load (id) retrievals.

The session usually provides a first level of "caching". In Hibernate the caching proper is called "second-level-cache", for this reason.
If you want to completely erase objects or object' collections from the cache, you call session.evict(<the object or collection>).

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 5:32 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
gonazao is correct, except if you're also using the query cache for your query in which case the objects in the object cache will be returned if the same query is executed twice.

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 6:21 pm 
Newbie

Joined: Mon Jan 07, 2008 8:26 pm
Posts: 7
Thanks for this. I've looked into it further after taking your comments on board and resolved the problem. This link also helped:

http://www.hibernate.org/hib_docs/nhibe ... ssioncache

Cheers!


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.