Decide whether each lock sequence obeys 2PL and find the lock point. T1: lock-X(A), read(A), write(A), lock-X(B), unlock(A), read(B), write(B), unlock(B). T2: lock-S(A), read(A), unlock(A), lock-S(B), read(B), unlock(B), display(A + B).
Concurrency control enforces isolation. Lock-based protocols (shared/exclusive locks, two-phase locking and its strict and rigorous forms), timestamp ordering, optimistic validation and MVCC each guarantee serializable schedules; deadlock handling, lock granularity and SQL isolation levels complete the picture.
The most common way to enforce isolation is locking. Before accessing a data item a transaction asks the lock manager for a lock; the lock manager records granted locks and queues of waiting requests in a lock table (usually a hash table keyed by item). Two basic modes exist.
| Requested ↓ / Held → | S (shared) | X (exclusive) |
|---|---|---|
| S — lock-S(Q), needed to read Q | ✓ granted | ✗ wait |
| X — lock-X(Q), needed to write Q (and read it) | ✗ wait | ✗ wait |
A request is granted when it is compatible with every lock other transactions hold on the item; otherwise the requester waits. To avoid starvation — a writer waiting forever while new readers keep arriving — a fair lock manager grants requests in arrival order, so a new S request queues behind a waiting X request. A transaction may upgrade S to X (when it decides to write) and downgrade X to S.
Every access in that schedule was properly locked, yet T2 saw an inconsistent state. Locks must be acquired and released according to a protocol; the best-known one is two-phase locking.
Theorem: if every transaction obeys 2PL, every schedule is conflict serializable, and ordering the transactions by their lock points gives an equivalent serial schedule. Intuitively, a transaction holds all its locks at its lock point, so any conflicting transaction must wait until it starts releasing — conflicts can only point one way. In the transfer example, 2PL forces T1 to lock B before unlocking A, so T2 can no longer slip in between.
| Variant | Rule | Serializable | Cascadeless | Deadlock-free |
|---|---|---|---|---|
| Basic 2PL | Growing then shrinking | Yes (conflict) | No — a transaction may read data whose writer later aborts | No |
| Strict 2PL | Basic 2PL + hold all X locks until commit/abort | Yes | Yes (strict schedules) | No |
| Rigorous 2PL | Hold all locks (S and X) until commit/abort | Yes — serial order = commit order | Yes | No |
| Conservative (static) 2PL | Acquire every needed lock before the first operation | Yes | Not by itself | Yes |
A deadlock exists when each transaction in a set waits for a lock held by another member of the set. Systems either detect and recover (let deadlocks happen, find them, break them) or prevent them (never let a cycle form). Most commercial systems detect.
| Ti requests an item held by Tj | Wait-die (non-preemptive) | Wound-wait (preemptive) |
|---|---|---|
| Ti is OLDER (smaller timestamp) than Tj | Ti waits | Ti wounds Tj — Tj is rolled back |
| Ti is YOUNGER (larger timestamp) than Tj | Ti dies — Ti is rolled back | Ti waits |
In both schemes only the younger transaction is ever rolled back, and it restarts with its original timestamp, so it eventually becomes the oldest and cannot starve. Wait-die can make a young transaction die repeatedly; wound-wait causes fewer rollbacks in practice because an old transaction never waits for a young one.
Instead of locks, each transaction gets a unique timestamp TS(Ti) when it starts (from the system clock or a counter); the protocol ensures that conflicting operations execute in timestamp order, so the equivalent serial schedule is the order of timestamps. Every data item Q carries W-timestamp(Q) (largest TS of a transaction that wrote Q successfully) and R-timestamp(Q) (largest TS that read Q).
| Step | Operation | Test | Outcome | Timestamps after |
|---|---|---|---|---|
| 1 | R1(A) | 10 ≥ W-TS(A) = 0 | Execute | R-TS(A) = 10 |
| 2 | R2(B) | 20 ≥ W-TS(B) = 0 | Execute | R-TS(B) = 20 |
| 3 | W2(A) | 20 ≥ R-TS(A) = 10 and ≥ W-TS(A) = 0 | Execute | W-TS(A) = 20 |
| 4 | R1(B) | 10 ≥ W-TS(B) = 0 | Execute | R-TS(B) stays 20 |
| 5 | W1(B) | 10 < R-TS(B) = 20 | Reject — roll back T1 | T1 restarts with a new timestamp |
Step 5 fails because the younger T2 has already read B; letting the older T1 write B now would mean T2 read the 'wrong' version in timestamp order. Properties: timestamp ordering guarantees conflict serializability and is deadlock-free (nobody waits), but long transactions may starve by being restarted repeatedly, and the basic protocol can produce unrecoverable schedules unless commits are delayed (strict timestamp ordering).
Robert Thomas (1979) observed that when TS(Ti) < W-TS(Q) — and the read test passed — Ti's write is obsolete: a younger transaction has already written Q, and in timestamp order Ti's value would be overwritten immediately anyway. Instead of rolling Ti back, simply ignore the write and let Ti continue.
The schedule R1(A) W2(A) W1(A) is not conflict serializable (edges T1 → T2 and T2 → T1) but is view serializable — Thomas' rule accepts exactly such schedules containing blind writes, so it allows more concurrency than basic timestamp ordering.
When conflicts are rare, checking at every access is wasted effort. An optimistic protocol lets transactions run without locks and checks for conflicts only at the end. Each transaction Ti passes through three phases and records Start(Ti), Validation(Ti) and Finish(Ti); TS(Ti) = Validation(Ti).
For each earlier-validated Ti, the validating Tj passes if one of these holds: (1) Ti finished its write phase before Tj started its read phase; (2) Ti finished writing before Tj starts writing, and WS(Ti) ∩ RS(Tj) = ∅; (3) Ti finished its read phase before Tj finished its read phase, and WS(Ti) ∩ RS(Tj) = ∅ and WS(Ti) ∩ WS(Tj) = ∅. Example: T1 has RS {A, B}, WS {B, C}; T2 has RS {A, D}, WS {A, B}. WS(T1) ∩ RS(T2) = ∅, but WS(T1) ∩ WS(T2) = {B} — if T1's write phase overlaps T2's, only condition 3 could apply and it fails, so T2 is rolled back.
UPDATE … WHERE version = :v.Multiversion schemes keep several versions of each item. A write creates a new version instead of overwriting; a read is served the version appropriate to the reader's timestamp or snapshot. Consequently readers never block writers and writers never block readers — the reason PostgreSQL, Oracle, MySQL InnoDB and SQL Server's snapshot modes all use MVCC.
Snapshot isolation prevents dirty reads, unrepeatable reads and lost updates but is not fully serializable: it allows write skew. Two doctors on call each check 'is the other one still on call?', both see yes in their snapshots, both go off duty, and nobody is on call. PostgreSQL's SERIALIZABLE level adds serializable snapshot isolation to catch such cases.
Items can be locked at different granularities: database → file/table → page → row (→ field). Coarse locks mean little lock overhead but poor concurrency; fine locks the opposite. Row-level locking lets concurrent transactions work on different rows of the same table. Multiple-granularity locking (Gray, 1975) lets each transaction choose, using intention modes on ancestors: IS (intends to S-lock something below), IX (intends to X-lock something below) and SIX (S on this node plus IX).
| Held → / Requested ↓ | IS | IX | S | SIX | X |
|---|---|---|---|---|---|
| IS | ✓ | ✓ | ✓ | ✓ | ✗ |
| IX | ✓ | ✓ | ✗ | ✗ | ✗ |
| S | ✓ | ✗ | ✓ | ✗ | ✗ |
| SIX | ✓ | ✗ | ✗ | ✗ | ✗ |
| X | ✗ | ✗ | ✗ | ✗ | ✗ |
Protocol: lock the root first and proceed top-down; to S- or IS-lock a node, hold IS or IX on its parent; to X-, SIX- or IX-lock a node, hold IX or SIX on its parent; release bottom-up; and follow 2PL. Updating one row of emp needs IX on the database, IX on emp and X on the row; a transaction wanting S on the whole emp table must then wait, because S and IX are incompatible. Systems may escalate many row locks into one table lock to save memory.
| Isolation level | Dirty read | Non-repeatable read | Phantom |
|---|---|---|---|
| READ UNCOMMITTED | Possible | Possible | Possible |
| READ COMMITTED | Prevented | Possible | Possible |
| REPEATABLE READ | Prevented | Prevented | Possible |
| SERIALIZABLE | Prevented | Prevented | Prevented |
SET TRANSACTION ISOLATION LEVEL …Defaults differ: Oracle, PostgreSQL and SQL Server use READ COMMITTED, MySQL InnoDB uses REPEATABLE READ, and SQLite transactions are SERIALIZABLE. Implementations may exceed the minimum — PostgreSQL's REPEATABLE READ is snapshot isolation and also prevents phantoms; Oracle offers only READ COMMITTED and SERIALIZABLE (plus READ ONLY). Lower levels trade correctness for concurrency.
Try the problem first, then reveal the working one step at a time.
Decide whether each lock sequence obeys 2PL and find the lock point. T1: lock-X(A), read(A), write(A), lock-X(B), unlock(A), read(B), write(B), unlock(B). T2: lock-S(A), read(A), unlock(A), lock-S(B), read(B), unlock(B), display(A + B).
Transactions T5, T10, T15 have timestamps 5, 10, 15 (smaller = older). T10 holds a lock on item Q. What happens when (a) T5 requests Q, (b) T15 requests Q — under wait-die and under wound-wait?
With TS(T1) = 10 and TS(T2) = 20, run (a) R1(A) R2(B) W2(A) R1(B) W1(B) under basic timestamp ordering, and (b) R1(A) W2(A) W1(A) under basic TO and under Thomas' rule.
T1 (validated first) has RS = {A, B}, WS = {B, C}; T2 has RS = {A, D}, WS = {A, B}. T2 validates while T1 is still in its write phase. Does T2 pass?
T1 holds an S lock on A. Then T2 requests S(A), T3 requests X(A) and T4 requests S(A), in that order. Which requests are granted immediately by a fair (FIFO) lock manager, and why is T4 not granted at once even though S is compatible with S?
Does this transaction obey 2PL? lock-S(A); read(A); lock-X(B); unlock(A); write(B); lock-X(C); write(C); unlock(B); unlock(C).
Under basic timestamp ordering, TS(T1) = 5 and TS(T2) = 8, and item Q currently has R-TS(Q) = 8 and W-TS(Q) = 5. Treating each case separately from this state, what happens if T1 issues write(Q)? If T2 issues write(Q)? If T1 issues read(Q)?
Using multiple-granularity locking, which locks does a transaction need to update one row of emp? A second transaction wants to read the entire emp table with one lock — what does it request, and is it granted?
The wait-for graph has edges T1 → T2, T2 → T3, T3 → T1 and T4 → T2. Is there a deadlock? Which transactions are involved, and how should it be resolved?
12 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.