Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Prevent row update - Concurrency

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.


- 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
Posts: 10336
Hibernate Eclipse IDE 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 ]
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic