aspose file tools*
The moose likes Object Relational Mapping and the fly likes Best Way to handle the table update(Concurrency) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Best Way to handle the table update(Concurrency)" Watch "Best Way to handle the table update(Concurrency)" New topic
Author

Best Way to handle the table update(Concurrency)

Unni Pillai
Ranch Hand

Joined: Aug 22, 2010
Posts: 35
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.




SCJP,SCWCD,SCEA
Thomas Kruse
Greenhorn

Joined: Dec 26, 2005
Posts: 14

What do you mean with "two processes", are there two applications running?
Unni Pillai
Ranch Hand

Joined: Aug 22, 2010
Posts: 35
one application but two different components (Which are totally independent) ie, one is webservice another one is a servlet
Thomas Kruse
Greenhorn

Joined: Dec 26, 2005
Posts: 14

Looks like hibernate is telling the truth: You have different transactions which are overlapping.
Try to reduce the duration a transaction is held open, this should prevent the concurrent changes on the same object by two transactions.
Unni Pillai
Ranch Hand

Joined: Aug 22, 2010
Posts: 35

Thanks Kruse for the reply.

I tried to minimize the duration, but was not able to avoid 100% , that is why I moved to "updating the column using HQL" strategy.

In stead of updating the whole row, using saveOrUpdate(entity), I am using HQL update statements like " update column_name where....." . This is moving away from ORM right?

I can't think any advantages of hibernate, if we are updating the whole row of the table ( ie, if we are updating all most all columns then object.save()) it make sense.

If I am only updating one column of a table, there is no sense of loading all the values to app and then saving all back.

Are these thoughts valid or am moving away from the real benefits of ORMs

Unni



 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Best Way to handle the table update(Concurrency)