Skip to main content
2 of 3
added 4 characters in body
Neil McGuigan
  • 8.6k
  • 5
  • 40
  • 56

What you are looking for is called Optimistic Concurrency Control.

Add an int column to your table called version. Start at 0.

Read a row:

select whatever, version from mytable where id=1

Results:

something, 0

Update a row:

update mytable set whatever = something_new, version = 1 where id = 1 and version = 0

If that row was not found, raise an exception.

If you use an ORM like Hibernate, you can just add a field with a @Version annotation and it will handle this for you.

Neil McGuigan
  • 8.6k
  • 5
  • 40
  • 56