Database Basics
Transaction T1 holds a shared lock on a row and is reading it. T2 requests a shared lock and T3 requests an exclusive lock on the same row. Which is correct for the period until T1 releases its lock?
- AT2 obtains its lock immediately, but T3 must wait
- BBoth T2 and T3 must wait
- CBoth T2 and T3 obtain their locks immediately
Show answer and explanation
Answer: A. T2 obtains its lock immediately, but T3 must wait
A shared lock is taken when reading data; other transactions can also take shared locks on the same data and read it at the same time, but they cannot take an exclusive lock for writing. An exclusive lock is taken when updating data, and while it is held, other transactions can take neither shared nor exclusive locks. In this example, T1's shared lock and T2's shared lock are compatible, so T2 obtains its lock immediately and can read. T3's exclusive lock is incompatible with T1's shared lock, so T3 must wait until T1 releases its lock (and, since T2 also holds a shared lock, until T2 releases its lock as well). Using locks in this way to prevent interference between concurrently running transactions is called concurrency control.
- A. Correct. Shared locks are compatible with each other, and an exclusive lock is incompatible with a shared lock.
- B. Shared locks are compatible with each other, so T2 does not wait.
- C. An exclusive lock is incompatible with a shared lock, so T3 must wait.