Boolean
Boolean

Reputation: 14664

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

Upvotes: 8

Views: 2307

Answers (1)

Paco
Paco

Reputation: 8381

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.

Upvotes: 11

Related Questions