Explain optimistic and pessimistic locking in JDBC. There are two kinds of locking available in JDBC. 1. Optimistic Locking 2. Pessimistic Locking
Optimistic Locking
Optimistic Locking locks the record only when updating takes place. It ensures that the locks are held between selecting, updating, or deleting rows. This process needs a way to ensure that the changes to data are not performed between the time of being read and being altered. The primary advantage of optimistic locking is , it minimizes the time for which a given resource is unavailable which is used by another transaction and in this way it more scalable locking alternative.
Pessimistic Locking
- Pessimistic Locking locks the records as soon as it selects the row to update. The pessimistic locking strategy assumes the probability of modifying a row that is used by one user can be used by another user for modifying. In this scenario, a lock is held between the row selection time and the updation time for changing operations on that row. The pessimistic locking strategy guarantees the changes are made safely and consistently.
|