Explain optimistic and pessimistic locking in JDBC

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.
What causes the "No suitable driver" error?
JDBC - No suitable driver - Failure to load the appropriate JDBC driver before giving a call to DriverManager.getConnection method causes ‘No Suitable Driver’ error...
Is the JDBC-ODBC Bridge multi-threaded?
JDBC Bridge - The JDBC-ODBC Bridge uses synchronized methods to serialize all of the calls made to ODBC. The concurrent access...
What is an SQL Locator?
JDBC SQL Locator - DBMS uses the logical pointers as identifiers to locate and manipulate the data. A Locator is an SQL3 data-type...
Post your comment