Two-Phase Locking (2PL) Protocol in Concurrency Control
Two-Phase Locking (2PL) Protocol in Concurrency Control
The protocol controls when locks can be acquired and when they can be released during a transaction.
1. Definition of Two-Phase Locking
A transaction follows the Two-Phase Locking protocol if:
All lock operations (
read_lock,write_lock) occur before the first unlock operation.
In other words, a transaction must finish acquiring all locks before it starts releasing any lock.
2. Two Phases of Two-Phase Locking
Every transaction executing under 2PL has two distinct phases.
| Phase | Description |
|---|---|
| Growing Phase | Transaction can acquire locks but cannot release any |
| Shrinking Phase | Transaction can release locks but cannot acquire new locks |
1. Growing Phase (Expanding Phase)
During this phase:
-
The transaction acquires locks on required data items
-
No locks are released
Example:
Locks are only added in this phase.
2. Shrinking Phase
During this phase:
-
The transaction releases locks
-
No new locks can be acquired
Example:
Once the transaction releases its first lock, it enters the shrinking phase permanently.
3. Lock Conversion Rules in 2PL
If lock conversion is allowed, it must follow strict rules.
Lock Upgrading
Changing a read lock → write lock
This must occur in the growing phase.
Lock Downgrading
Changing a write lock → read lock
This must occur in the shrinking phase.
4. Why Two-Phase Locking is Needed
Simple locking does not guarantee serializability.
Problem example:
-
Transaction T1 unlocks Y too early
-
Transaction T2 unlocks X too early
This allows interleaving that produces a nonserializable schedule, leading to incorrect results.
Two-Phase Locking prevents this by forcing transactions to finish locking before unlocking.
5. Guarantee of Serializability
If every transaction in a schedule follows the Two-Phase Locking protocol, then:
-
The schedule is guaranteed to be conflict serializable
-
There is no need to test serializability separately
Thus, the protocol itself ensures correctness.
6. Limitations of Two-Phase Locking
Although 2PL guarantees serializability, it introduces some limitations.
1. Reduced Concurrency
A transaction may have to keep locks longer than necessary.
Example:
-
Transaction finishes using item X
-
But it cannot release X yet because it still needs to lock Y
This forces other transactions waiting for X to wait longer.
2. Some Serializable Schedules Are Disallowed
Even though a schedule might be serializable, 2PL may not allow it.
Thus, 2PL is correct but restrictive.
7. Variants of Two-Phase Locking
There are several variations of the 2PL protocol designed to improve performance or avoid problems like deadlocks.
1. Basic Two-Phase Locking (Basic 2PL)
This is the standard form of the protocol.
Rules:
-
Transaction follows growing phase and shrinking phase
-
Locks can be released anytime during shrinking phase
Characteristics:
-
Guarantees conflict serializability
-
May still cause deadlocks
2. Conservative Two-Phase Locking (Static 2PL)
In this variation:
-
A transaction locks all required data items before it begins execution
The transaction must declare:
-
Read-set → items it will read
-
Write-set → items it will write
Example:
If any item cannot be locked:
-
The transaction does not start
-
It waits until all items become available.
Advantage
-
Deadlock-free
Because all locks are acquired at the beginning.
Disadvantage
-
Requires knowing all items needed before execution
-
Not always possible in practice.
3. Strict Two-Phase Locking (Strict 2PL)
This is the most commonly used protocol in real DBMS systems.
Rule:
A transaction cannot release any write (exclusive) lock until it commits or aborts.
Thus:
-
Write locks are held until transaction completion
Advantages
-
Prevents cascading rollbacks
-
Produces strict schedules
-
Improves recoverability
Disadvantage
-
Deadlocks can still occur
4. Rigorous Two-Phase Locking (Rigorous 2PL)
This is a stricter version of Strict 2PL.
Rule:
A transaction does not release any lock (read or write) until it commits or aborts.
Thus:
-
Both shared locks and exclusive locks are held until completion.
Advantages
-
Guarantees strict schedules
-
Easier to implement
Disadvantage
-
Even less concurrency compared to strict 2PL.
8. Comparison of 2PL Variants
| Variant | Lock Release Rule | Deadlock | Concurrency |
|---|---|---|---|
| Basic 2PL | Locks released in shrinking phase | Possible | Moderate |
| Conservative 2PL | All locks acquired before start | No deadlock | Lower |
| Strict 2PL | Write locks held until commit | Possible | Moderate |
| Rigorous 2PL | All locks held until commit | Possible | Lowest |
9. How the DBMS Implements 2PL
Usually the concurrency control subsystem automatically issues lock requests.
Example:
If transaction executes:
The system automatically performs:
Similarly:
becomes
The system checks the lock table:
-
If the item is locked → transaction waits
-
If available → lock is granted
The lock table is then updated accordingly.
10. Problems of Lock-Based Concurrency Control
Locking introduces two major problems.
1. Deadlock
Two transactions wait for each other’s locks.
Example:
2. Starvation
A transaction waits indefinitely because other transactions keep getting priority.
Final Summary
Two-Phase Locking is a locking protocol that guarantees serializability of schedules.
Key points:
-
Transactions have two phases: growing and shrinking.
-
Locks are acquired first and released later.
-
It ensures conflict serializable schedules.
Main variants:
-
Basic 2PL – standard form
-
Conservative 2PL – locks all items before starting
-
Strict 2PL – holds write locks until commit
-
Rigorous 2PL – holds all locks until commit
Two-phase locking is one of the most widely used concurrency control techniques in database systems.


Comments
Post a Comment