Lecture: Lecture 17 - Concurrency Control
Related: Two-Phase Locking and Strict 2PL, Concurrency Anomalies and Serializability Theory

How deadlocks happen (Slides 75–76)

Deadlocks are a natural consequence of locking systems where transactions hold locks while waiting for additional locks. In the classic two-transaction cycle from Slide 76:

TimeState
1bot, r_lock₁(x), r₁(x) holds R-Lock(x)
2bot, r_lock₂(y), r₂(y) holds R-Lock(y)
3w_lock₁(y) → wait waits for to release y
4w_lock₂(x) → wait waits for to release x

Neither transaction can progress or release its held lock, so both remain blocked forever unless the DBMS intervenes.

Three ways to handle deadlocks (Slides 77–79)

1. Timeout (Slide 77) — Standard in commercial DBMSs

Each lock request specifies a maximum waiting time (TimeOut). If the timer expires while the transaction is still waiting, the lock manager removes it from the queue, returns Not Ok, and the transaction performs a rollback (releasing its locks so others can proceed).

  • Commercial adoption: Typically adopted in commercial DBMSs because it is simple and cheap (no global graph maintenance).
  • Tuning tradeoff:
    • Too long: Deadlocked transactions wait unnecessarily long before being resolved.
    • Too short: Causes overkill—aborts and restarts healthy transactions that were simply waiting briefly and were not actually in a deadlock, overloading the system.

2. Deadlock prevention (Slide 78)

Stop waiting cycles from ever forming:

  • Pessimistic 2PL: A transaction must acquire all locks it will ever need before starting execution.
    • Limitation: Not always feasible, because a transaction often does not know every object or row it will access until earlier queries execute.
  • Timestamp ordering: Assign each transaction a start timestamp and allow only “younger” (or only “older”) transactions to wait for the other; conflicting requests in the disallowed direction trigger an abort instead of a wait.
    • Limitation: May cause overkill (unnecessary rollbacks when no deadlock actually existed).
FlavorWhen locks are acquiredWhen locks are releasedDeadlocks possible?
Basic 2PL (Slide 48)Gradually, before each accessAnytime, with no new locks after first unlockYes
Strict 2PL (Slide 53)Gradually, before each accessAll together after COMMIT / ROLLBACKYes
Pessimistic 2PL (Slide 78)All upfront before startingAfter execution / commitNo (prevented, but often infeasible)

3. Deadlock detection (Slide 79)

Allow deadlocks to happen, then detect and break them using a wait graph (wait-for graph):

  • Nodes: Active transactions ().
  • Directed edge : is currently waiting for a lock held by .
  • Deadlock condition: A cycle in the wait graph () proves a deadlock, and one transaction in the cycle is rolled back.
  • Tradeoff: Expensive to build and maintain continuously in centralized high-throughput engines, so it is used primarily in distributed DBMSs (where timeouts across network nodes are harder to tune).

Common confusion

Do not confuse the Conflict Graph (Slides 33–35) with the Wait Graph (Slide 79):

  • Conflict Graph: Tests conflict serializability (CSR) of a schedule; edges record RW/WR/WW operation ordering regardless of whether any transaction waited.
  • Wait Graph: Detects deadlocks at runtime; edges mean is currently suspended waiting for a lock held by .

Retrieval questions

1. Why do most commercial centralized DBMSs choose Timeout over Wait-For Graphs and Pessimistic 2PL, and what happens if the timeout threshold is set too low?
Timeout avoids the runtime overhead of continuously maintaining and cycle-checking a wait-for graph, and unlike Pessimistic 2PL it does not require knowing every needed lock before the transaction begins. Setting the timeout too short causes overkill—rolling back and restarting non-deadlocked transactions, which overloads the system.

2. In Slide 76, why didn’t fail immediately when it acquired r_lock₁(x) and acquired r_lock₂(y)?
Because x and y were different objects and both were Free. The deadlock only formed later when each transaction needed an exclusive W-Lock on the object the other was still holding under Strict 2PL.

3. How do the edges of a Conflict Graph differ from the edges of a Wait Graph?
A Conflict Graph edge represents a conflicting data access (RW, WR, or WW) where ‘s operation preceded ‘s; a Wait Graph edge means is currently blocked waiting for a lock held by .