posted 13 years ago
Hi All,
I am using hibernate for data layer in my current project . I have a typical problem to solve.
I have two process one to submit a job to a workflow and another one to do the job. both of these processes update same table but different columns , 'submitted' and 'completed' respectively.
As per the current implementation, both the processes are using saveOrUpdate(entity) method of the HibernateTemplate( Spring Util class).
We never noticed the issue before because , if there are large number of items to be processed, there will not be any time conflict between the process to update the table.
If there is few records to be processed, the submit process loads the entity in the session, do some business logic, at the same time the same process grabs the same record and updates it. when the submit process try to save the record we are getting.
20:35:00,378 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect):
It clearly says the version is out of sync , which makes complete sense.
The solution what we found is to write a HQL statement to update those columns (sumbitted, completed) and not to load the entire object in to session. This approach will avoid the version conflict issue.
My question is is this the right way of using hibernate? are we moving away from hibernate.
I am pretty sure there are more complicated applications out there , which uses hibernate, solves issues about concurrency.
Any insight is appreciated.