Site icon DataSagar Blog

Two-phase locking technique in DBMS concurrency control – An Introduction

Two-phase locking is a concurrency control technique used in database management systems (DBMS) to ensure the serializability of transactions. Serializability is the property that ensures that the concurrent execution of transactions produces the same result as if the transactions were executed one at a time.

Two-phase locking is based on the idea of acquiring locks on data items before reading or modifying them, and releasing locks after the data has been read or modified. There are two phases in the two-phase locking protocol:

  1. Growing phase: During this phase, a transaction can acquire locks on data items, but cannot release any locks.
  2. Shrinking phase: During this phase, a transaction can release locks, but cannot acquire any new locks.

Here is an example of how two-phase locking works:

In this example, Transaction 2 is executed after Transaction 1, even though they were running concurrently. This ensures that the integrity of the database is preserved as if the transactions were executed one at a time.

Here is an algorithm for two-phase locking:

  1. Begin the transaction and enter the growing phase.
  2. Acquire locks on any data items that need to be accessed or modified.
  3. Execute the transaction.
  4. Release all locks and enter the shrinking phase.
  5. Commit the transaction.

Two-phase locking is a simple and effective concurrency control technique, but it can suffer from performance issues due to lock contention (when multiple transactions are trying to acquire the same lock).

Exit mobile version