0% found this document useful (0 votes)
58 views4 pages

Two Phase Locking Protocol

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views4 pages

Two Phase Locking Protocol

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Two Phase Locking Protocol

Last Updated : 30 Jul, 2025


The Two-Phase Locking (2PL) Protocol is a key technique used in DBMS to
manage how multiple concurrent transactions access and modify data. When
many users or processes interact with a database, it’s important to ensure that
data remains consistent and error-free. Without proper management, issues
like data conflicts or corruption can occur
The Two-Phase Locking Protocol resolves this issue by defining clear rules for
managing data locks. It divides a transaction into two phases:
1. Growing Phase: In this step, the transaction gathers all the locks it needs
to access the required data. During this phase, it cannot release any locks.
2. Shrinking Phase: Once a transaction starts releasing locks, it cannot
acquire any new ones. This ensures that no other transaction interferes
with the ongoing process.
Types of Lock
 Shared Lock (S): Shared Lock is also called a read-only lock, allows
multiple transactions to access the same data item for reading at the same
time. However, transactions with this lock cannot make changes to the
data. A shared lock is requested using the lock-S instruction.
 Exclusive Lock (X): An Exclusive Lock allows a transaction to both read
and modify a data item. This lock is exclusive, meaning no other
transaction can access the same data item while this lock is held. An
exclusive lock is requested using the lock-X instruction.
Read more about the Locks.

Lock Conversions
In the Two-Phase Locking Protocol, lock conversion means changing the type
of lock on data while a transaction is happening. This process is carefully
controlled to maintain consistency in the database.
1. Upgrading a Lock: This means changing a shared lock (S) to an exclusive
lock (X). For example, if a transaction initially only needs to read data (S)
but later decides it needs to update the same data, it can request an
upgrade to an exclusive lock (X). However, this can only happen during the
Growing Phase, where the transaction is still acquiring locks.
Example: A transaction reads a value (S lock) but then realizes it needs to
modify the value. It upgrades to an X lock during the Growing Phase.
2. Downgrading a Lock: This means changing an exclusive lock (X) to a
shared lock (S). For instance, if a transaction initially planned to modify
data (X lock) but later decides it only needs to read it, it can downgrade the
lock. However, this must happen during the Shrinking Phase, where the
transaction is releasing locks.
Example: A transaction modifies a value (X lock) but later only needs to
read the value, so it downgrades to an S lock during the Shrinking Phase.
2PL-Locking
These rules ensure that the Two-Phase Locking Protocol maintains
consistency and avoids conflicts between transactions. By limiting when
upgrades and downgrades can occur, the system prevents situations where
multiple transactions interfere with each other’s operations.
Read more about Shared and Exclusive Locks.
Let's see a transaction implementing 2-PL.
T1 T2

1 lock-S(A)

2 lock-S(A)

3 lock-X(B)

4 .......... ..........

5 Unlock(A)

6 Lock-X(C)

7 Unlock(B)

8 Unlock(A)

9 Unlock(C)

10 .......... ..........

This is a basic outline of a transaction that demonstrates how locking and


unlocking work in the Two-Phase Locking Protocol (2PL).
Transaction T1
 The growing Phase is from steps 1-3
 The shrinking Phase is from steps 5-7
 Lock Point at 3
Transaction T2
 The growing Phase is from steps 2-6
 The shrinking Phase is from steps 8-9
 Lock Point at 6
Lock Point
The lock point in a transaction is the moment when the transaction finishes
acquiring all the locks it needs. After this point, no new locks can be added,
and the transaction starts releasing locks. It’s a key step in the Two-Phase
Locking Protocol to ensure the rules of growing and shrinking phases are
followed.
Example of 2PL
Imagine a library system where multiple users can borrow or return books.
Each action (like borrowing or returning) is treated as a transaction. Here's
how the Two-Phase Locking Protocol (2PL) works, including the lock point:
User A wants to:
1. Check the availability of Book X.
2. Borrow Book X if it's available.
3. Update the library's record.
Growing Phase (Locks are Acquired):
1. User A locks Book X with a shared lock (S) to check its availability.
2. After confirming the book is available, User A upgrades the lock to an
exclusive lock (X) to borrow it.
3. User A locks the library's record to update the borrowing details.
Lock Point: Once User A has acquired all the necessary locks (on Book X and
the library record), the transaction reaches the lock point. No more locks can
be acquired after this.
Shrinking Phase (Locks are Released):
1. User A updates the record and releases the lock on the library's record.
2. User A finishes borrowing and releases the exclusive lock on Book X.
This process ensures that no other user can interfere with Book X or the
library record during the transaction, maintaining data accuracy and
consistency. The lock point ensures that all locks are acquired before any are
released, following the 2PL rules.
Drawbacks of 2PL
Two-phase locking (2PL) ensures that transactions are executed in the correct
order by using two phases: acquiring and releasing locks. However, it has
some drawbacks:
 Deadlocks: Transactions can get stuck waiting for each other’s locks,
causing them to freeze indefinitely.
 Cascading Rollbacks: If one transaction fails, others that depend on it
might also fail leading to inefficiency and potential data issues.
 Lock Contention: Too many transactions competing for the same locks can
slow down the system, especially when many users are working at the
same time.
 Limited Concurrency: The strict rules of 2PL can reduce how many
transactions can run at once, resulting in slower performance and longer
wait times.
Cascading Rollbacks in 2-PL
Let's see the following Schedule:
Schedule
The image illustrates a transaction schedule using the Two-Phase Locking
(2PL) protocol, showing the sequence of actions for three transactions T1, T2
and T3.

You might also like