Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to code optimistic and pessimistic locking from java code

I know what optimistic and pessimistic locking is, but when you write a java code how do you do it? Suppose I am using Oracle with Java, do I have any methods in JDBC that will help me do that? How will I configure this thing? Any pointers will be appreciated.

like image 743
user2434 Avatar asked Jan 01 '12 03:01

user2434


People also ask

How is optimistic locking implemented in Java?

In order to use optimistic locking, we need to have an entity including a property with @Version annotation. While using it, each transaction that reads data holds the value of the version property. Before the transaction wants to make an update, it checks the version property again.

What is optimistic locking and pessimistic locking in Java?

There are two models for locking data in a database: Optimistic locking , where a record is locked only when changes are committed to the database. Pessimistic locking , where a record is locked while it is edited.

How do you handle optimistic locking exception in Java?

Solution. To resolve this error we have two ways: Get the latest object from the database and set the old object values if you need those values to be persisted to the new object and merge it. For the old object set the latest version from Database.

How do you get pessimistic locking in hibernate?

Pessimistic locking in hibernate PessimisticLockException will be thrown when we query for rows which are already locked. If the initial select query is successful, rows which meet the select query criteria are locked for the duration of a transaction. We can be sure that no other transaction will modify them.


2 Answers

You can implement optimistic locks in your DB table in this way (this is how optimistic locking is done in Hibernate):

  1. Add integer "version" column to your table.
  2. Increase the value of this column with each update of corresponding row.
  3. To obtain lock, just read "version" value of the row.
  4. Add "version = obtained_version" condition to where clause of your update statement. Verify number of affected rows after update. If no rows were affected - someone has already modified your entry.

Your update should look like

UPDATE mytable SET name = 'Andy', version = 3 WHERE id = 1 and version = 2

Of course, this mechanism works only if all parties follow it, contrary to DBMS-provided locks that require no special handling.

Hope this helps.

like image 140
Andrei Petrenko Avatar answered Oct 15 '22 01:10

Andrei Petrenko


Suppose I am using Oracle with Java, do I have any methods in JDBC that will help me do that?

This Oracle paper should provide you with some tips on how to do this.

There are no specific JDBC methods. Rather, you achieve optimistic locking by the way that you design your SQL queries / updates and where you put the transaction boundaries.

like image 33
Stephen C Avatar answered Oct 15 '22 02:10

Stephen C