Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best Way to handle the table update(Concurrency)

 
Unni Pillai
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Thomas Kruse
Greenhorn
Posts: 14
Firefox Browser Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean with "two processes", are there two applications running?
 
Unni Pillai
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one application but two different components (Which are totally independent) ie, one is webservice another one is a servlet
 
Thomas Kruse
Greenhorn
Posts: 14
Firefox Browser Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic