100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is the Two-Phase Locking (2PL) Protocol?

Learn how the Two-Phase Locking protocol guarantees serializability, and the difference between basic, strict, and rigorous 2PL.

hardQ43 of 228 in Database Est. time: 6 minsLast updated:
Open Code Lab

Expected Interview Answer

Two-Phase Locking (2PL) is a concurrency-control protocol that guarantees serializable execution by splitting every transaction into a growing phase, where it can only acquire locks and never release any, and a shrinking phase, where it can only release locks and never acquire any new ones.

Because a transaction is forbidden from acquiring a new lock once it has released even one, all lock acquisitions happen before any lock release, which prevents interleavings that would violate serializability. Plain 2PL still allows other transactions to read or write data that was locked and released before commit, which can lead to cascading rollbacks if the releasing transaction later aborts; Strict 2PL fixes this by holding all write (exclusive) locks until the transaction commits or aborts, and Rigorous 2PL holds all locks, read and write, until commit or abort. Most production relational databases use Strict or Rigorous 2PL under the hood for lock-based isolation levels, trading some concurrency for the guarantee that committed data is never rolled back due to another transaction’s failure.

  • Guarantees conflict-serializable schedules
  • Strict 2PL avoids cascading rollbacks from uncommitted reads
  • Forms the theoretical basis for how most lock-based databases enforce isolation
  • Makes the trade-off between concurrency and correctness explicit and provable

AI Mentor Explanation

Think of a team manager who, once selection meetings begin, can only add new players to the squad list and is forbidden from removing anyone until the growing phase ends — that is the "acquiring locks" phase. Once the manager finalizes and starts removing names to trim the squad, no more players may be added; only removals happen from then on. Two-phase locking works identically: a transaction can only acquire new locks during its growing phase, and once it releases even a single lock, it can never acquire another, guaranteeing the selection process never interleaves additions with removals in a way that breaks consistency.

Step-by-Step Explanation

  1. Step 1

    Enter the growing phase

    The transaction acquires whatever shared or exclusive locks it needs as it executes, but releases none.

  2. Step 2

    Reach the lock point

    The moment the transaction releases its first lock, the growing phase ends and no further locks may be acquired.

  3. Step 3

    Enter the shrinking phase

    The transaction may only release locks from this point forward, never acquire new ones.

  4. Step 4

    Choose strict or rigorous variants in practice

    Strict 2PL holds write locks until commit/abort to avoid cascading rollbacks; Rigorous 2PL holds all locks until commit/abort.

What Interviewer Expects

  • Correct statement of the growing-phase/shrinking-phase rule
  • Understanding that 2PL guarantees serializability, not deadlock-freedom
  • Distinction between basic 2PL, Strict 2PL, and Rigorous 2PL
  • Awareness that plain 2PL can still cause cascading rollbacks, and how Strict 2PL avoids it

Common Mistakes

  • Saying 2PL prevents deadlocks (it does not; it guarantees serializability, deadlocks still require detection or prevention)
  • Confusing the two phases with two separate transactions instead of one transaction’s lifecycle
  • Not distinguishing basic 2PL from Strict/Rigorous 2PL
  • Forgetting that once a lock is released, no new lock can ever be acquired in that transaction

Best Answer (HR Friendly)

Two-phase locking means a transaction goes through two clean phases: first it can grab as many locks as it needs but never let go of any, and then, once it releases even one lock, it can only keep releasing from then on, never grab a new one. That strict split is what mathematically guarantees the transactions behave as if they ran one after another, even though they are running concurrently. Most real databases use a stricter version that holds locks until commit to avoid one transaction’s failure cascading into another’s rollback.

Code Example

Conceptual 2PL lock timeline (Strict 2PL, illustrative)
-- Growing phase: acquire locks, never release
BEGIN TRANSACTION;
SELECT * FROM Accounts WHERE account_id = 1 FOR UPDATE; -- acquire lock on row 1
SELECT * FROM Accounts WHERE account_id = 2 FOR UPDATE; -- acquire lock on row 2
UPDATE Accounts SET balance = balance - 100 WHERE account_id = 1;
UPDATE Accounts SET balance = balance + 100 WHERE account_id = 2;

-- Shrinking phase begins only at commit under Strict 2PL:
-- all locks are held until this point, then released together.
COMMIT;
-- Under Strict 2PL, write locks are released only at COMMIT/ROLLBACK,
-- preventing another transaction from reading uncommitted, lockable data.

Follow-up Questions

  • What is the difference between Strict 2PL and Rigorous 2PL?
  • Does two-phase locking prevent deadlocks? Why or why not?
  • How does 2PL relate to conflict serializability?
  • How does 2PL compare to MVCC as a concurrency-control strategy?

MCQ Practice

1. In Two-Phase Locking, what happens after a transaction releases its first lock?

Releasing the first lock marks the start of the shrinking phase, during which no new locks may be acquired.

2. What guarantee does Two-Phase Locking provide?

2PL guarantees conflict-serializable schedules; it does not by itself prevent deadlocks, which need separate detection or prevention.

3. How does Strict 2PL differ from basic 2PL?

Strict 2PL holds exclusive locks until the transaction ends, preventing other transactions from seeing uncommitted writes and avoiding cascading rollbacks.

Flash Cards

What is Two-Phase Locking?A protocol where a transaction only acquires locks in a growing phase and only releases them in a shrinking phase.

What does 2PL guarantee?Conflict-serializable execution of concurrent transactions.

Strict 2PL vs basic 2PL?Strict 2PL holds write locks until commit/abort, avoiding cascading rollbacks that basic 2PL can suffer.

Does 2PL prevent deadlocks?No — it guarantees serializability, not deadlock freedom; deadlocks need separate handling.

1 / 4

Continue Learning