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
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.
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.
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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With