Locking & Two-Phase Locking
Locks are the classic way to enforce isolation. Two-phase locking makes the schedule serializable, and deadlocks are the price.
In plain English
One key to the bathroom. Others wait. If two people each hold one key and wait for the other, that is a deadlock.
The one idea
Two-phase locking guarantees conflict serializability by never releasing a lock before the transaction has acquired all the locks it will need.
It is the classic proof that a schedule can be made safe by controlling access to items. It also explains why databases deadlock and why they must detect cycles.
By the end
- 01Explain shared and exclusive locks.
- 02Read a lock compatibility matrix.
- 03Describe growing and shrinking phases.
- 04Recognize and prevent deadlock.
Shared and exclusive locks
A shared lock allows other transactions to read but not write. An exclusive lock allows one transaction to read and write. Multiple shared locks can coexist; an exclusive lock conflicts with everything. The lock manager grants or blocks requests according to a compatibility matrix.
S X
S yes no
X no noTwo-phase locking
In the growing phase, a transaction acquires locks and may not release any. In the shrinking phase, it releases locks and may not acquire new ones. Strict 2PL holds all locks until commit, which prevents cascading rollbacks and guarantees serializability.
Deadlock
A deadlock happens when transactions wait on each other in a cycle. The DBMS can prevent it with ordered locking, avoid it with wait-die or wound-wait, or detect it by building a wait-for graph and aborting a victim. Deadlock is a correctness-preserving cost, not data corruption.
T1 holds A, waits for B
T2 holds B, waits for A
wait-for graph: T1 -> T2 -> T1 (cycle)A deadlock in two steps
T1 needs A then B. T2 needs B then A. Both run at the same time.
T1 locks A; T2 locks B.
Each transaction holds one item and is making progress.
T1 requests B; T2 requests A.
Each waits for the other; the wait-for graph contains a cycle.
The DBMS aborts one transaction.
The survivor acquires both locks, commits, and releases them.
ConclusionThe growing phase acquires locks; the shrinking phase releases them. Strict 2PL holds exclusive locks until commit, which also prevents dirty reads.
Instrument
Watch it happen.
Step through one operation at a time
T1 acquires a shared lock on A
A shared lock allows reads and is compatible with other shared locks.
The trade-off
No free correctness.
Gain
Serializable schedules from local lock rules.
Cost
Blocking, deadlocks, and reduced concurrency; lock granularity changes the pressure.
Verdict
Use strict 2PL or a proven variant, and always have a deadlock detector.
Recap
What to keep.
Takeaways
- +Shared locks allow readers; exclusive locks allow one writer.
- +2PL has a growing phase and a shrinking phase.
- +Strict 2PL holds locks until commit.
- +Deadlock is a cycle in the wait-for graph.
- +Deadlock can be prevented, avoided, or detected.
Traps
- !Releasing a lock in the growing phase.
- !Assuming 2PL prevents deadlock; it prevents non-serializable schedules.
- !Ignoring lock granularity and contention.
Focus
- →Use the compatibility matrix.
- →Trace growing and shrinking phases.
- →Draw a wait-for graph and identify a deadlock.
Check yourself
Before you turn the page.
Which lock conflicts with every other lock?
What is the defining rule of 2PL?