What is the difference between optimistic locking and pessimistic locking


Answer posted by Mohan on 2005-05-21 17:20:02: Optimistic locking assumes that no one would read or change the data while changes are being by a bean 
Pessimistic locking would rather lock down the data so that no one can access it

Showing Answers 1 - 5 of 5 Answers

Mohan

  • May 21st, 2005
 

Optimistic locking assumes that no one would read or change the data while changes are being by a bean 
Pessimistic locking would rather lock down the data so that no one can access it

  Was this answer useful?  Yes

John

  • May 27th, 2005
 

In optimistic locking the row will not be locked until the actual update is performed. In order to prevent the lost update issue a version number column will be used. In pessimistic locking the row will be locked at the time when the data is selected from the database. This will prevent other users who want to update the same record. This should not prevent the readers but this behaviour varies from database to database.

  Was this answer useful?  Yes

Optimistic Locking

Is based on the assumption that it is unlikely that separate users will access the same object simultaneously.  The EJB does not hold a lock for the duration of the transaction, just for the small period of time the EJB interacts with the DB. If db detects a collision, the transaction rolls back. Suitable for large systems requiring significant concurrent access.


Pessimistic locking:

The EJB holds a lock for the duration of the transaction, not allowing any other updates to the data. Suitable for small scale system where concurrent access is
rare.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions