-->
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.  [ 2 posts ] 
Author Message
 Post subject: How do I recover from a StaleObjectStateException?
PostPosted: Fri Mar 17, 2006 8:08 pm 
Newbie

Joined: Thu Mar 16, 2006 1:12 pm
Posts: 10
Hi all,

I'm using Hibernate3 and I'm trying to port my non-hibernate application to use hibernate. I'm trying to implement optimistic locking and am running into a problem.

When my application first starts, it loads a particular "object" from the DB. I then have the application "go to sleep" for 10 seconds while I go to the DB and manually modify the version number for the row that the object maps to (I'm trying to force a StaleObjectStateException). My application then "wakes up", does a save() and then calls commit(). During the commit, I get a StaleObjectStateException - which makes sense since I modified the version in the DB while the application was asleep. My application attempts to "recover" from this by "re-reading" the object from the DB. I've tried load() and get() - but you can tell it's just getting the object out of the cache - even though the object is stale (hibernate writes no select sql to the console). Is there something that I need to call in order to get Hibernate to re-fetch this item from the DB? Shouldn't it do this automatically? Do I have to nuke my entire cache? Do I have to close my session?

Enquiring minds want to know.

Thanks for you assistance!

-john


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 8:26 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
there are a few things to make sure to deal with:

1) hibernate is behaving correctly by throwing the Stale exception... agreed.
Yet if your Session gets any exception whatsoever, you must close/rollback and must restart a new session.

2) if you want to ensure that your objects are up to date then you call session.refresh(...); and it will requery, but again you must do this prior to hitting the Exception - you can setup hibernate to refresh prior to and DML in your mapping, however this is a big performance hit obviously.

Lastly you can roll your own way of notifying hibernate that an external process updated the database, and hibernate needs to refresh its cache (2nd level cache, if you set it up for that class and/or collection) - probably some JMS publish/subscribe fired on a database trigger or some other monitoring thread. You can refresh the 2nd level cache by "sessionfactory.evict*()" methods. And then when the objects are loaded/or queried they will be 2nd level cached again.

If you haven't setup any 2nd level cache yet, and you're only considering 1st level cache then it might get tricky but probably doable to notify the live 1st level caches (Session) to refresh its objects.. but this again would be risky...ie, what if it were in the middle of a transaction, you'd have to rollback?...etc.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


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