0 votes
in Hibernate by
What is the difference between optimistic and pessimistic locking in hiberntae ?

1 Answer

0 votes
Best answer

Optimistic locking assumes that multiple transactions can complete without affecting each other. Optimistic means “optimistic” (आशावादी) view. If a conflict occurs, the transaction must be aborted and repeated. Hibernate supports optimistic locking typically maintain a version field and raise an exception (StaleObjectException) if one tries to update an object with an outdated version number.

You add a version field to your table and a version element to your mapping file (*.hbm.xml or Annotation mapping). When you update a Hibernate object Hibernate will check the version value. If they do the update is successful, and the version is incremented, otherwise not  and Hibernate throws a "StaleObjectException".

General Life Example:
Optimistic locking is like leaving the door to the toilet open. You have a number of toilets, select one, and you are optimistic that nobody will come to use the same toilet, too. If someome comes, it will of course be embarassing, an exception is raised, and he must abort his try, but you hope that these conflicts are the execption.

Pessimistic locking means the “pessimistic” view. It is highly likely to corrupt each other’s data, and that the only safe option is to lock the database and serialize data access, so at most one user has control of any piece of data at one time. This ensures data integrity, but reduces speed and the amount of concurrent activity the system can support.

Pessimistic locking should not be used for web applications as it creates pending transactional state in the database in the form of row-level locks. If pessimistic locking is set, state management will work, but the locking mode will not perform as expected. Behind the scenes, every time an application module is recycled, a rollback is issued in the JDBC connection.

1. If a user selects a record for update, and then leaves for lunch without finishing or aborting the transaction. All other users that need to update that record are forced to wait until the user returns and completes the transaction, or until the DBA kills the offending transaction and releases the lock.
2. The Deadlock – Users A and B are both updating the database at the same time. User A locks a record and then attempt to acquire a lock held by user B – who is waiting to obtain a lock held by user A.

General Life Example:
Pessimistic locking is like always locking the door to the toilet: although there are a number of different toilets, you are expecting every time that somebody else will come to use one, too, and that the only safe option is to lock all toilet doors (which corresponds to page locking). If someone comes and wants to use a toilet, he cannot enter and must wait before the locked door.

Share:- Whatsapp Facebook Facebook

Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.