Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate optimistic locking..how it works?

I was reading the below blog about hibernate optimistic locking. I am planning to use it with hibernate. But, I have one concern. we have java code and c++ code, both connect to one database. While, java code can use hibernate to achieve optimistic locking, I want to make the c++ code do the same thing. Also, c++ code is using some legacy code.

http://turgaykivrak.wordpress.com/2009/05/16/72/

Is there a documentation that explains how hibernate achieves optimistic locking?

Any suggestions are appreciated.

Thank you
Bala

like image 270
Boolean Avatar asked Jul 27 '10 20:07

Boolean


People also ask

How does Hibernate optimistic locking work?

1.2 Optimistic Locking in Hibernate Version checks the version numbers or the timestamps to detect conflicting updates and to prevent lost updates. In here, A record is only locked while updating and when it is updated, hibernate increments the version count by one.

What is optimistic and pessimistic locking in Hibernate?

The lock exists until the transaction has either been committed or rolled back. With optimistic locking, a resource is not actually locked when it is first is accessed by a transaction. Instead, the state of the resource at the time when it would have been locked with the pessimistic locking approach is saved.

What is pessimistic locking in Hibernate?

Pessimistic. Pessimistic locking assumes that concurrent transactions will conflict with each other, and requires resources to be locked after they are read and only unlocked after the application has finished using the data. Hibernate provides mechanisms for implementing both types of locking in your applications.

How do you stop optimistic locking in Hibernate?

If you use optimistic locking, Hibernate uses a version column to keep track of the current version of the entity and to prevent concurrent modifications. You, therefore, need to make sure that your client always updates its representation of the entity after the user triggered any change on the entity.


1 Answers

To be precise, you don't mean optimistic locking, but optimistic concurrency (without a lock). Using a timestamp for version is just for legacy database support, because a modern database can (at least theoretically) work faster than it's accuracy of storing a timestamp.

Using the integer version property is very simple:

  • On insert: set version to 1
  • On update and delete: increase version with 1 and append "where version=@version" to every sql statementent. Return the number of changed records. Throw a StaleObjectStateException when the number of changed records is different than expected.

Personally, I would not create two separate applications writing the same data in a non-legacy situation, because that means that business logic have to be duplicated and changes have to be applied to two applications, also when the change is relevant for only one of the applications.

like image 178
Paco Avatar answered Oct 21 '22 21:10

Paco