CS 527
Home
B+ Tree
Bloom Filter
Hash Tables
Buffer Pool
Join Algorithms
External Sort
2PL & Locking
Two-Phase Locking & Concurrency
2PL guarantees conflict-serializable schedules. Growing phase: acquire locks. Shrinking phase: release locks. No lock acquisition after first release.
Schedule Simulator
Lock Compatibility
Deadlock Detection
S
Shared (read)
X
Exclusive (write)
⊗
Denied
t = 0
Select a schedule and step through it to see 2PL in action.
Lock Manager Table
Granted locks / Waiting
Object → [holders] / [waiters]
No locks held
Schedule
Basic Read Conflict
Write-Write Conflict
2PL Serializable
Deadlock Scenario
Reset
Step Controls
← Prev
t = 0
Next →
▶ Auto Play
2PL Rules
Growing Phase:
• Acquire any lock
• Cannot release yet
Shrinking Phase:
• Release locks
• Cannot acquire new locks
Strict 2PL:
• Hold ALL locks until
commit/abort
• Prevents cascading aborts
Compatibility Matrix
Req\Held
S-LOCK
X-LOCK
none
S-LOCK
✓
✗
✓
X-LOCK
✗
✗
✓
Lock Compatibility Demonstrator
Multiple transactions can hold S-locks simultaneously. An X-lock requires exclusive access.
Transaction T1
S-Lock(A)
X-Lock(A)
Release(A)
S-Lock(B)
X-Lock(B)
Release(B)
Transaction T2
S-Lock(A)
X-Lock(A)
Release(A)
S-Lock(B)
X-Lock(B)
Release(B)
Lock State
No locks held
Click buttons to request/release locks on objects A and B.
Reset All Locks
Rules
S + S:
Both granted ✓
S + X:
X must wait ✗
X + S:
S must wait ✗
X + X:
Second must wait ✗
Shared = read-only
Exclusive = read + write
Upgrade: S → X only if
you are the sole S-holder
Build a waits-for graph. A cycle = deadlock.
Lock State
No locks
Waits-For Graph
No waits
Run a scenario to see how deadlocks form and how they are detected.
Deadlock Scenarios
2-Transaction
3-Transaction
Detect & Abort Victim
Reset
Detection Methods
Waits-For Graph:
Build directed graph: Ti→Tj if Ti waits for Tj to release a lock.
Deadlock ↔ cycle in graph.
Resolution:
• Abort youngest txn (victim)
• Or use timeouts
Prevention:
• Wait-Die: older waits, younger dies
• Wound-Wait: older wounds younger