We have a web app using Hibernate 3.2.6 that uses automatic versioning for all objects using a timestamp as the version attribute. We have two cases where we consistently get StaleObjectStateExceptions when we know we are the only user accessing a particular piece of data. The exceptions are thrown when Hibernate tries to flush an object that has been modified in the current session more than once. That is, the flow is:
1. Object loaded from database. 2. Object modified. 3. Object flushed automatically by Hibernate in preparation for a query on another object class. 4. Object modified some more. 5. Attempt to flush object gets StaleObjectStateException, as we prepare to do another query on a different object class.
So, I conclude that the first flush updates the version (timestamp), and the second flush fails because it reads the database value for the version, not the one already in the session.
This same pattern fails if steps 1 and 2 are a new object creation and save, as opposed to a load of an existing object.
This doesn't seem like proper behavior to me. Shouldn't the version check use the value for the object that's in the session, instead of looking to the database to do its check?
Any suggestions for a work around?
Thanks for your help.
|