Handling concurrency using optimistic locking technique
Join the DZone community and get the full member experience.Join For Free
Most of the modern persistence frameworks like Hibernate, Ibatis, JPA, etc suppport automatic versioning at row level. A hibernate sample will look like,
< class name="foo" optimistic-lock="version" table="FOOS">
< version column="version" name="version" type="int">
When there is a update operation on the FOOS table,
(1) Hibernate issues an update statement like
UPDATE FOOS SET BAR='bar', VERSION=2
WHERE ID=1234 AND VERSION=1
(2) Hibernate checks the JDBC row count, and throws a StaleObjectStateException if no rows were updated
You don't have to worry much about other things. Most of the current persistence framework will automatically update/check the version number during each update.
And moreover, the classic Synchronized,Lock,Wait,etc approach is tied to that specific JVM.If you are running a distributed application across different JVM's,you have more things to worry about..