Thursday, August 13, 2026
Home Academic Two-phase locking technique in DBMS concurrency control - An Introduction

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.

To Make It More Clear:

Two-Phase Locking (2PL) Protocol

Ensures Conflict-Serializable Schedules in DBMS Concurrency Control

Phase 1: Growing Phase

  • ✔ ALLOWED: Transaction may obtain locks.
  • ❌ FORBIDDEN: Transaction may NOT release locks.

Phase 2: Shrinking Phase

  • ✔ ALLOWED: Transaction may release locks.
  • ❌ FORBIDDEN: Transaction may NOT obtain locks.
Visual Lock Count Profile
Lock 1
Lock 2
Lock 3
Lock Point
Release
Release
◄ Growing Phase ► Locked Phase ◄ Shrinking Phase ►
The Lock Point Rule: The protocol assures serializability. Transactions are serialized in the exact order of their Lock Points (the specific moment a transaction acquires its final lock).

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

Two-Phase Locking (2PL) Execution Example
Step Transaction 1 (T1) Transaction 2 (T2) Lock Manager State
Step 1 Begins & Acquires Lock(D1) 🔒 D1 LOCKED by T1
Step 2 Executes on D1… Begins & Tries Lock(D1) ⛔ T2 BLOCKED (D1 held by T1)
Step 3 Releases Lock(D1)
Acquires Lock(D2)
WAITING… 🔓 D1 UNLOCKED
🔒 D2 LOCKED by T1
Step 4 Executes on D2… Acquires Lock(D1) & Begins Execution 🔒 D1 LOCKED by T2 (Unblocked)
2PL Note: Step 3 represents T1 entering its Shrinking Phase for D1 while simultaneously continuing lock actions on D2.

Simply,

  • Transaction 1 begins and acquires a lock on data item D1.
  • Transaction 2 begins and tries to acquire a lock on D1, but is blocked because Transaction 1 has a lock on D1.
  • Transaction 1 releases its lock on D1 and acquires a lock on D2.
  • Transaction 2 acquires the lock on D1 and begins executing.

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:

Simple Way (Easy for non-technical Students to Understand)

STEP 1 Transaction Initialization: Begin the transaction and officially enter the Growing Phase.
STEP 2 Lock Acquisition: Acquire all necessary locks on data items that need to be accessed or modified. (No locks may be released during this phase).
STEP 3 Execution: Safely execute the transaction operations while holding all acquired locks.
STEP 4 Lock Release: Release all locks and transition into the Shrinking Phase. (No new locks may be acquired during this phase).
STEP 5 Completion: Commit the transaction permanently to the database.

Algorithm for two-phase locking – Technical One (Works for IT Related Courses)

Algorithm: Basic Two-Phase Locking (2PL)
// Phase 1: Growing Phase
BEGIN_TRANSACTION (T);
SET_STATE (T, “GROWING”);

FOR EACH data_item X REQUIRED BY T:
  ACQUIRE_LOCK (T, X);
END FOR;

// Execution & Peak Lock Point
EXECUTE_OPERATIONS (T);

// Phase 2: Shrinking Phase
SET_STATE (T, “SHRINKING”);
FOR EACH lock L HELD BY T:
  RELEASE_LOCK (T, L);
END FOR;

COMMIT_TRANSACTION (T);

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).

Happy Learning!
– DataSagar

datasagarhttp://www.DataSagar.com
Sagar is multidisciplinary technologist, educator, and entrepreneur based in Nepal. As the founder of Illionso Technologies and Kashi Garden Resort, he operates at the intersection of web architecture, data intelligence, innovation, and digital transformation. From building digital solutions to scaling real-world concepts, his mission is to share knowledge alongside merge emerging as well as disruptive technologies with transformative offline experiences.
RELATED ARTICLES

Why Linux Won the Infrastructure War? Lessons from Linus Torvalds

In 1991, 21-year-old Linus Torvalds announced a "hobby" operating system. Today, Linux powers supercomputers, cloud servers, and Android. Explore his journey, the open-source economy, and the lessons for modern developers.

What Is Vibe Coding? A Complete Beginner’s Guide to Building Software and Web Pages With AI

A few years ago, building a website meant sitting down with a programming book, picking a language, and slowly working through syntax...

What is Answer Engine Optimization (AEO)? Getting Ready for AI Powered Search and Agentic Era Digital Marketing

Search is changing faster than it has in the last two decades. For years, businesses focused on ranking their websites on search...

Most Popular

Why Linux Won the Infrastructure War? Lessons from Linus Torvalds

In 1991, 21-year-old Linus Torvalds announced a "hobby" operating system. Today, Linux powers supercomputers, cloud servers, and Android. Explore his journey, the open-source economy, and the lessons for modern developers.

What Is Vibe Coding? A Complete Beginner’s Guide to Building Software and Web Pages With AI

A few years ago, building a website meant sitting down with a programming book, picking a language, and slowly working through syntax...

What is Answer Engine Optimization (AEO)? Getting Ready for AI Powered Search and Agentic Era Digital Marketing

Search is changing faster than it has in the last two decades. For years, businesses focused on ranking their websites on search...

The Plain Text Web: Why llms.txt Is Becoming the New Site Standard

The web built today is heavy. Between client-side JavaScript, cookie banners, tracker scripts, and responsive CSS grids, a typical webpage carries megabytes...