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 typeGrantsCoexists withRequested by
Shared (S)read onlyother S locksREAD
Exclusive (X)read + writenothingUPDATE

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)

TimeOperationX
0 (Init)โ€”
1T1 reads X; T2 reads X
2T1:
3T2: (from stale )
4T1 commit
5T2 commit โŸน T1โ€™s update lost

2. S/X Locking with 2PL

TimeTxnOpAB
0T1READ AS(T1)โ€”
1T2READ AS(T2)โ€”
2T1UPDATE AT1 WAIT T2โ€”
3T2READ Bโ€”S(T2)
4T2UPDATE Bโ€”X(T2)
5T2COMMIT โŸน releases; T1 getsX(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