When I try to commit my session and another user also changed that same object, then Hibernate throws a StaleObjectStateException
.
When I try to recover from that exception I call:
session.refresh(<staleObject>)
Then I once again call the commit that previously failed. But the staleObjectStateException
occurs once again on the same object. Even if there are no changes made by another user, I also verified that the version number of the object is the same as the number in our DB.
Anyone know what might be wrong? Does anyone have a better idea on how to gracefully handle the exception without closing and opening a new session?
note: we use a Single Session per View implementation for our desktop (RCP) application
As you have mentioned its a long running session, as per hibernate doc for refresh()
method,
Re-read the state of the given instance from the underlying database. It is inadvisable to use this to implement long-running sessions that span many business tasks. This method is, however, useful in certain special circumstances. For example where a database trigger alters the object state upon insert or update after executing direct SQL (eg. a mass update) in the same session after inserting a Blob or Clob
I would suggest you to change the strategy of using the single session across the application life cycle, and use session but transaction meaning open session only when you need to interact with database and once done, close the sesion.
I am not ware of any compulsions you have for using long running session, this is just a suggestion.