Two-Phase Locking (2PL) Protocol in Concurrency Control

 

Two-Phase Locking (2PL) Protocol in Concurrency Control

Two-Phase Locking (2PL) is one of the most important concurrency control protocols in a DBMS.
It ensures that concurrent transaction schedules are serializable, meaning they produce the same result as some serial execution.

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.

PhaseDescription
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:

read_lock(X) write_lock(Y) read_lock(Z)

Locks are only added in this phase.


2. Shrinking Phase

During this phase:

  • The transaction releases locks

  • No new locks can be acquired

Example:

unlock(Z) unlock(Y) unlock(X)

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

read_lock(X) write_lock(X)

This must occur in the growing phase.


Lock Downgrading

Changing a write lock → read lock

write_lock(X) read_lock(X)

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:

Lock all required items Start transaction execution

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

VariantLock Release RuleDeadlockConcurrency
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:

read_item(X)

The system automatically performs:

read_lock(X) read_item(X)

Similarly:

write_item(X)

becomes

write_lock(X) write_item(X)

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:

T1 holds X and waits for Y T2 holds Y and waits for X

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

Popular posts from this blog

Database Management Systems DBMS PCCST402 Semester 4 KTU CS 2024 Scheme

Data Models, Schemas and Instances

Introduction to Database Management System -DBMS