Lecture: Lecture 17 - Concurrency Control
Related: Concurrency Anomalies and Serializability Theory, Hierarchical Locking Predicate Locking and SQL2 Isolation Levels, Deadlock Handling in Concurrency Control, Distributed Transaction Classification and Two-Phase Commit

Locks and the Lock Manager (Slides 39–47)

Because testing VSR is NP-complete and maintaining runtime conflict graphs is too expensive, commercial DBMSs enforce serializability using a lock manager (acting as the scheduler). A lock blocks conflicting access to a resource (Slide 39):

  • Read lock (R-Lock): Shared among multiple transactions reading the same object. The lock manager maintains a counter of active readers; the object returns to Free only when count = 0 (Slide 46).
  • Write lock (W-Lock): Exclusive—not compatible with any other R-Lock or W-Lock on the same object (Slide 40).
  • Lock escalation (Slide 40): Requesting a W-Lock on data for which the transaction already holds an R-Lock.

Each read is preceded by an R-Lock request and eventually followed by Unlock; each write is preceded by a W-Lock request and followed by Unlock.

Lock table and Conflict Table (Slides 42–47)

The lock manager consults two in-memory structures (Slides 42, 47):

  • Lock table (in main memory): stores 2 bits per data object to represent the 3 states (free, r_locked, w_locked) plus a counter of waiting transactions.
  • Conflict table (Slides 44–45): defines the outcome (Result / New Resource State) for each request:
Request \ Resource StateFreeR-LockedW-Locked
R-LockOk / R-LockedOk / R-Locked (shared)No / W-Locked (wait)
W-LockOk / W-LockedNo / R-Locked (wait)No / W-Locked (wait)
UnlockErrorOk / It depends (Free if no other R-Lock)Ok / Free

When a lock is not granted (No), the requesting transaction is placed in a waiting state until the resource is unlocked and becomes available (Slide 41).

Two-Phase Locking (2PL) (Slides 48–52)

Acquiring and releasing locks one operation at a time does not ensure serializability (unlocking y immediately after reading y would still allow Ghost Update (a)). Two-Phase Locking (2PL) enforces two phases per transaction (Slides 48–49):

  1. Growing phase: The transaction acquires locks gradually, right before each read or write it needs, without releasing any lock yet.
  2. Shrinking phase: The transaction releases its locks—once a transaction releases any lock, it can never acquire a new lock.
Locked resources
   ▲        ___________
   │       /           \
   │      / Growing     \ Shrinking
   │     / (acquire)     \ (release only!)
   └────/─────────────────\────────► Time

Guarantee and subset hierarchy (Slides 49–50)

Every schedule accepted by 2PL is guaranteed to be conflict serializable:

Worked source example — CSR, but NOT 2PL (Slide 50):

  • Why CSR? Conflict edges are (on x) and (on y). No cycle (), so is CSR.
  • Why not 2PL? For to access x, must have already released its lock on x (entering its shrinking phase). Later, must acquire a new lock on y for , violating 2PL.

Step-by-step trace: How 2PL prevents Ghost Update (a) (Slides 51–52)

Initial state: x = 400, y = 300, z = 300 (sum 1000). reads x, y, z to sum them; transfers 100 from y to z.

StepActionResource lock states (x, y, z)Explanation
1: r_lock₁(x), r₁(x)x: 1 (read)Granted; keeps x locked
2: r_lock₂(y), r₂(y)x: 1 (read), y: 2 (read)Granted
3: r_lock₁(y), r₁(y)x: 1 (read), y: 1,2 (read)Shared R-Lock granted! holds x and y
4: r_lock₂(z), r₂(z)x: 1, y: 1,2, z: 2 (read)Granted
5: w_lock₂(y)T₂ waits (y still held by 1,2)Exclusive W-Lock blocked while holds R-Lock on y
6: r_lock₁(z), r₁(z)x: 1, y: 1,2, z: 1,2 (read)Shared R-Lock granted; reads old z = 300 → total 1000
7: commit, unlock₁(x), unlock₁(y)x: free, y: 2 (write)Once unlocks y, wakes up, gets w_lock₂(y), writes w₂(y)
8: w_lock₂(z) (waits), : unlock₁(z)x: free, y: 2 (write), z: 2 (write) unlocks z → writes w₂(z), commit, unlocks y and z

Because 2PL prevented from unlocking y before reading z, could not modify y and z mid-read, preserving the exact result of the serial schedule .

Strict 2PL: Removing the commit projection assumption (Slide 53)

In basic 2PL, if writes x and releases its W-Lock before committing, could read the uncommitted value and then could ABORT—causing a Dirty Read and cascade rollback.

  • Strict 2PL rule: A transaction may release its locks only at the very end of the transaction, after COMMIT or ROLLBACK.
  • Consequence: All data visible to other transactions is already stable (committed), allowing us to drop the commit projection hypothesis and completely preventing Dirty Reads.

Added study trace: gradual acquisition with x, y, z

(Added example outside the PDF to illustrate gradual lock acquisition in Strict 2PL): Suppose asks for x, then y; asks for z; then asks for z.

  • If both and only read z: holds r_lock₂(z) and ‘s r_lock₁(z) is granted immediately (shared reads)—neither waits, and each unlocks all its locks together at COMMIT.
  • If holds w_lock₂(z) (or wants w_lock₁(z)): acquires x and y as it reaches them, then waits on z while continuing to hold x and y. Once commits and calls unlock₂(z), wakes up, acquires z, finishes, commits, and releases x, y, z together. (If had instead requested w_lock₂(x) while holding z, both would wait on each other—creating a deadlock.)

Lock manager service interface, conflict probability, and timeouts (Slides 54–57)

  • Primitives (Slide 54): R-Lock(T, x, ErrorCode, TimeOut), W-Lock(T, x, ErrorCode, TimeOut), UnLock(T, x), where ErrorCode returns Ok or Not Ok and TimeOut caps wait duration.
  • Waiting queues (Slides 55–56): Satisfied requests return control with tiny delay; blocked requests suspend the transaction in a waiting queue and resume the head of the queue when the resource unlocks.
  • Conflict probability approximation (Slide 56):

    where is the number of active transactions, is the average number of objects accessed per transaction, and is the total number of objects in the database.
  • Timeout expiration (Slide 57): When TimeOut expires, the lock manager removes the waiting transaction from the queue, resumes it, and returns Not Ok. The transaction may perform a rollback (and possibly restart) or retry the lock request later without releasing its already-held locks.

Common confusion

Basic/Strict 2PL vs. Pessimistic 2PL: In normal 2PL and Strict 2PL, a transaction does not grab every lock at the start of the transaction—it acquires each lock on demand right before accessing that item, and simply holds onto acquired locks until the shrinking phase (basic 2PL) or COMMIT/ROLLBACK (Strict 2PL). Grabbing all locks upfront before starting is Pessimistic 2PL (a deadlock-prevention scheme).

Retrieval questions

1. Transaction writes x, unlocks x immediately, and commits 5 seconds later without requesting any other locks. Does it satisfy basic 2PL and Strict 2PL?
It satisfies basic 2PL (it entered its shrinking phase and acquired no further locks), not Strict 2PL (it released x before COMMIT). During those 5 seconds, if another transaction reads x and aborts, a Dirty Read occurs.

2. Using the conflict formula , why does switching from table-level locks to tuple-level locks reduce conflicts?
Tuple-level locking dramatically increases (the number of lockable objects in the database), which shrinks .

3. Why is conflict serializable yet rejected by 2PL?
Its conflict graph () is acyclic, so it is CSR. However, had to unlock x so could access x and then later lock y for —acquiring a lock after releasing a lock violates 2PL.