Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prevent row update - Concurrency

 
Pradeep Kadambar
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am relatively new to Hibernate. I have a API which is inherently support concurrency.

API

- Check user exists
- If no, do something
- Save/update user

As post API call I persist the user to the DB with a 'Save or Update' call.

The problem is - 'If two threads try to work on the same user simultaneously, both thread will find out the user does not exist. Do the logic and try to save the user to the DB. At the end 1st thread will do some update and the 2nd thread will overwrite the updates of the first'.

Can I solve this by using 'vesion' or 'timestamp' ?

Craving for suggestions
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If both threads are performing inserts you don't have a lost update problem. You'll get a primary key exception.

If you are performing updates, you have a candidate for optimistic locking, so you can use Hibernate's versioning functionality.
[ November 05, 2008: Message edited by: Paul Sturrock ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic