Concurrency Control and Locking
Context: FIT2094_MOC ยท enforces the Isolation of ACID ยท stops interleaved transactions corrupting shared data Parent Framework: Database Transaction
Quick Revision
- ๐ฏ Objective: let multiple transactions interleave safely โ guard each data item with shared/exclusive locks under two-phase locking.
- ๐ฆ Core Components: S/X locks โ granularity (db/table/page/row) | 2PL โ growing then shrinking.
- โก Key Constraint: the lost update โ interleaved reads then writes overwrite each other; serial execution avoids it but throttles throughput.
๐ Core
1. The Concurrency Problem (Lost Update)
- Serial โ finish T1 entirely before T2 โ guarantees isolation but low throughput.
- Interleaved (non-serial) โ alternate operations across transactions โ high throughput but may break isolation.
- Lost update โ two transactions read the same value, both compute from the stale copy, and the later commit overwrites the earlier โ one update vanishes.
2. Locks โ Shared (S) and Exclusive (X)
- Lock โ marks a data item temporarily unavailable to other transactions.
- Shared (S) โ read-only; many transactions may hold S on the same item at once.
- Exclusive (X) โ read+write; only one holder, and no other lock may coexist.
- Granularity โ database / table / page / row โ finer = more concurrency, coarser = simpler.
- Rule of thumb โ READ โน S, UPDATE โน X; COMMIT/ROLLBACK releases all locks.
3. Two-Phase Locking (2PL)
- Growing phase โ acquire all needed locks (no release yet); once held, apply changes.
- Shrinking phase โ issue COMMIT/ROLLBACK, then release locks โ never acquire after releasing.
โ๏ธ Core Decision Matrix
| Lock type | Grants | Coexists with | Requested by |
|---|---|---|---|
| Shared (S) | read only | other S locks | READ |
| Exclusive (X) | read + write | nothing | UPDATE |
When It Flips: an X request must wait until every other lock (S or X) on the item is released; multiple S locks are compatible, so readers never block readers โ only a writer forces the wait.
๐ Exam Execution Trace
1. Lost Update (no locking)
| Time | Operation | X |
|---|---|---|
| 0 (Init) | โ | |
| 1 | T1 reads X; T2 reads X | |
| 2 | T1: | |
| 3 | T2: (from stale ) | |
| 4 | T1 commit | |
| 5 | T2 commit โน T1โs update lost |
2. S/X Locking with 2PL
| Time | Txn | Op | A | B |
|---|---|---|---|---|
| 0 | T1 | READ A | S(T1) | โ |
| 1 | T2 | READ A | S(T2) | โ |
| 2 | T1 | UPDATE A | T1 WAIT T2 | โ |
| 3 | T2 | READ B | โ | S(T2) |
| 4 | T2 | UPDATE B | โ | X(T2) |
| 5 | T2 | COMMIT โน releases; T1 gets | X(T1) | โ |
Final Extracted Output: T1โs X-lock on A is blocked until T2 commits and drops its S(A); only then does T1 acquire X(A).
๐ง Active Recall
At time 2 T1 wants to UPDATE A but must wait โ why, and when is it unblocked?
- Hint: X needs all other locks released.
Answer
- Short answer: T1 needs an X-lock on A, but T2 still holds S(A); X is incompatible with any other lock, so T1 waits until T2 COMMITs (time 5) and releases.
- Why: S-many, X-one โ shared locks coexist, an exclusive lock demands sole possession.
How does two-phase locking prevent the lost update?
- Hint: Acquire-all-before-release.
Answer
- Short answer: Under 2PL a transaction takes an X-lock before writing and holds it (growing phase) until commit; the second transaction cannot read/write the item until the first releases, so no write is based on a stale value.
- Why: Growing then shrinking โ no lock is released while more are still being acquired, serialising the conflicting accesses.