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.
|