Sub topic 3
Two-Phase Locking and Its Variants
This chapter assumes you know basic SQL (SELECT, UPDATE), what a transaction is, and how cycles appear in a directed graph.
Two-phase locking
Two-phase locking (2PL) is a rule that a transaction acquires locks in a growing phase and releases them in a shrinking phase. After its first release, it may not acquire another lock. This rule guarantees conflict serializability.
- Basic 2PL lets a transaction release locks before it commits. Other transactions can then read uncommitted data, so cascading aborts are possible.
- Strict 2PL holds exclusive locks until commit or abort. This prevents dirty reads and yields strict schedules.
- Rigorous 2PL holds all locks, shared and exclusive, until commit or abort. Serialization order then matches commit order.
Deadlock with wait-die and wound-wait
A wait-for graph has an edge from Ti to Tj when Ti waits for a lock that Tj holds. A cycle in this graph is a deadlock. Wait-die and wound-wait avoid cycles by comparing timestamps, where a smaller timestamp means an older transaction.
def wait_die(requester_ts, holder_ts):
return 'wait' if requester_ts < holder_ts else 'abort requester'
def wound_wait(requester_ts, holder_ts):
return 'abort holder' if requester_ts < holder_ts else 'wait'
print(wait_die(5, 9)) # wait
print(wait_die(9, 5)) # abort requester
print(wound_wait(5, 9)) # abort holder
print(wound_wait(9, 5)) # waitTimestamp ordering and the Thomas write rule
Each data item stores a read timestamp and a write timestamp. A transaction with a timestamp older than the item's write timestamp cannot read it, so that transaction aborts. A write is rejected when a younger transaction has already read the item.
The Thomas write rule relaxes one case. If a write is older than the item's write timestamp, and no younger transaction has read the item, the write is obsolete. The system skips it instead of aborting the transaction.
SQL isolation levels
Isolation levels set which anomalies a transaction may see.
| Isolation level | Dirty read | Unrepeatable read | Phantom |
|---|---|---|---|
| READ UNCOMMITTED | Possible | Possible | Possible |
| READ COMMITTED | Not possible | Possible | Possible |
| REPEATABLE READ | Not possible | Not possible | Possible |
| SERIALIZABLE | Not possible | Not possible | Not possible |
Multiple granularity and intention locks
A database can lock at the table level or the row level. An intention lock (IS or IX) on a table tells other transactions that some row inside is locked. A table lock request then checks one intention lock instead of scanning every row.
Multiversion and optimistic concurrency
Multiversion concurrency control (MVCC) keeps several versions of each item. A read sees a consistent snapshot and does not block writers. Optimistic control lets transactions run without locks, then checks for conflicts at commit and aborts on a conflict.
Exercise
Under strict 2PL, a transaction holds an exclusive lock on A. It reads A for the last time, releases the lock on A, and then requests a lock on B. Is this allowed?
Show answer
No. Strict 2PL holds exclusive locks until commit or abort, so releasing A early is not allowed.