Quick revision
The short answer
Two-phase locking is a rule for concurrency control: a transaction first acquires all the locks it needs (the growing phase) and, once it releases any lock, may never acquire another (the shrinking phase). Strict 2PL holds exclusive locks until commit or abort, which avoids cascading aborts, and that is what lock-based databases implement. Because conflicting locks force conflicting operations into one order, every schedule it allows is conflict-serializable. Two-phase commit is unrelated: it is a protocol for committing atomically across several nodes.
What to remember
In this answer, we saw that two-phase locking splits a transaction into a growing phase that only acquires locks and a shrinking phase that only releases them, which orders all conflicts by lock point and makes every schedule conflict-serializable. Strict 2PL holds exclusive locks until commit or abort to avoid cascading aborts and is what lock-based databases use; range locks such as InnoDB's next-key locks stop phantoms. The costs are blocking between readers and writers and deadlocks, and two-phase commit is a separate atomic commit protocol.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Two-phase locking is a rule for concurrency control: a transaction first acquires all the locks it needs (the growing phase) and, once it releases any lock, may never acquire another (the shrinking phase). Strict 2PL holds exclusive locks until commit or abort, which avoids cascading aborts, and that is what lock-based databases implement. Because conflicting locks force conflicting operations into one order, every schedule it allows is conflict-serializable. Two-phase commit is unrelated: it is a protocol for committing atomically across several nodes.
Why this question is asked
Most of us use databases that rely on MVCC, so two-phase locking can sound like history. It is not: row locks for writes, SELECT ... FOR UPDATE, SQL Server's default locking and InnoDB's SERIALIZABLE all follow it, and it is the clearest way to understand why a schedule is serializable. Interviewers ask it, and add 'do not confuse it with two-phase commit', because the names are so similar that many candidates mix them up. In this answer, we will recap locks and conflicts, define basic and strict 2PL, see why it guarantees serializability, cover phantoms and costs, and finish by separating it from two-phase commit.
1A Quick Recap of Locks and Conflicts
A lock is a reservation on a piece of data. A shared lock, S, is taken for reading; many transactions may hold S on the same row at once. An exclusive lock, X, is taken for writing; only one transaction may hold it, and nobody else may hold S or X on that row meanwhile. Two operations conflict when they come from different transactions, touch the same data and at least one is a write. A conflicting request waits until the lock is released.
| Held \ Requested | Shared (S) | Exclusive (X) |
|---|---|---|
| Shared (S) | granted | waits |
| Exclusive (X) | waits | waits |
2Basic Two-Phase Locking
Locks alone do not give serializability; when we release them matters. Suppose T1 reads A, releases it, and then later locks B, while T2 changes A and B in between. T1 would see the old A and the new B, a mix no serial order produces. Two-phase locking forbids this with one rule: a transaction has a growing phase, during which it may acquire locks and may not release any, and a shrinking phase, which starts with its first release and during which it may not acquire any more.
The moment a transaction holds all its locks, just before it releases the first, is called its lock point.
3Why 2PL Guarantees Serializable Schedules
Consider any two conflicting operations from T1 and T2 on the same row. Because the locks conflict, one transaction must release its lock before the other can get it. Say T1 releases first. T1 released that lock in its shrinking phase, so T1 had already passed its lock point; T2 got the lock in its growing phase, before its own lock point. So T1's lock point comes before T2's. The same argument holds for every conflicting pair, so all conflicts point in the order of the transactions' lock points.
That means the schedule is conflict-equivalent to running the transactions one by one in lock-point order, which is what conflict-serializable means. There can be no cycle of conflicts, because lock points are moments in time and time has no cycles.
4Strict 2PL and Cascading Aborts
Basic 2PL allows releasing an exclusive lock before commit. That creates a danger: T1 writes A, releases X(A), and T2 reads the new A. If T1 then aborts, T2 has read data that never existed, so T2 must abort too, and anything that read T2's writes must also abort. This chain is a cascading abort, and it also makes recovery hard.
Strict 2PL fixes it by holding every exclusive lock until the transaction commits or aborts. Nobody can read a write until it is final. Strong strict 2PL, also called rigorous 2PL, holds shared locks until the end as well, which makes the serial order simply the commit order. Real lock-based databases implement the strict or strong strict variant; they never release a write lock early.
5Phantoms Need Range Locks
Row locks protect rows that exist. They cannot protect a query's result against new rows that appear. If T1 runs SELECT count(*) FROM booking WHERE room = 5 and locks the three matching rows, T2 can still insert a fourth booking for room 5, a phantom. To be fully serializable, a lock-based database must lock the range or predicate the query covered. InnoDB does this with gap locks and next-key locks on index ranges; SQL Server uses key-range locks at SERIALIZABLE.
6The Costs, and Not Confusing It With Two-Phase Commit
Under 2PL, readers block writers and writers block readers, so a long report holding S locks stops updates to everything it read. Transactions waiting for each other can form a cycle, a deadlock, so the database needs a deadlock detector that aborts one victim. These costs are why most modern databases use MVCC for reads and keep locks for writes.
Two-phase commit, 2PC, solves a different problem: making one transaction commit on several nodes or databases atomically. A coordinator first asks every participant to prepare and promise it can commit, and only when all have voted yes tells them to commit. Its two phases are prepare and commit; 2PL's two phases are acquiring and releasing locks. A distributed transaction may use both, but neither is a version of the other.
7Follow-up questions
Which databases still use locking for SERIALIZABLE?
SQL Server implements SERIALIZABLE with locks held to the end of the transaction plus key-range locks. MySQL InnoDB at SERIALIZABLE turns plain SELECTs into locking reads (like FOR SHARE) when autocommit is off, so it is lock based with next-key locks. Db2 also uses locking. PostgreSQL instead uses Serializable Snapshot Isolation, and Oracle's SERIALIZABLE is snapshot isolation, so neither is 2PL for reads, though both use exclusive row locks for writes.
Is 2PL deadlock free?
No. Two transactions can each hold one lock and request the other's. Conservative 2PL, which acquires every lock before starting, avoids deadlocks but needs to know all locks in advance, which is rarely practical.
Does MVCC make 2PL obsolete?
Not entirely. MVCC handles reads, but writes in PostgreSQL, Oracle and InnoDB still take exclusive row locks held to commit, which is strict 2PL applied to writers.
8Conclusion
In this answer, we saw that two-phase locking splits a transaction into a growing phase that only acquires locks and a shrinking phase that only releases them, which orders all conflicts by lock point and makes every schedule conflict-serializable. Strict 2PL holds exclusive locks until commit or abort to avoid cascading aborts and is what lock-based databases use; range locks such as InnoDB's next-key locks stop phantoms. The costs are blocking between readers and writers and deadlocks, and two-phase commit is a separate atomic commit protocol.
Open with
Two-phase locking is a rule about when a transaction may take and release locks. Reads take shared locks, writes take exclusive locks, and conflicting requests wait. The rule is that a transaction has a growing phase where it only acquires locks, and once it releases any lock it enters the shrinking phase and can't acquire another.
Then, if they want more
That guarantees conflict-serializable schedules: for any two conflicting operations, one transaction released before the other acquired, which means its lock point, the moment it held all its locks, came first. So every conflict points in lock-point order and there can't be a cycle.
Basic 2PL can release a write lock before commit, so another transaction may read data that is later rolled back and have to abort too, a cascading abort. Strict 2PL holds exclusive locks until commit or abort, and strong strict holds all locks, and that's what lock-based databases actually implement.
To stop phantoms you also need range or predicate locks, like InnoDB's next-key locks. The costs are that readers and writers block each other and deadlocks are possible, so the database needs deadlock detection. And it's not two-phase commit, which is a prepare-then-commit protocol for committing atomically across several nodes.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is two-phase locking (2PL), how does strict 2PL differ from basic 2PL, and why does...": https://scrivio-interview-library.netlify.app/databases/acid-and-isolation/a9fb20f219/