Database Basics
Which correctly describes two-phase locking (2PL), used for concurrency control of transactions?
- AA transaction may alternate between acquiring and releasing locks any number of times. This guarantees that deadlocks never occur
- BLocks are taken only for writes, not for reads. This prevents every problem, including dirty reads
- COnce a transaction releases any lock, it acquires no new locks afterwards. This guarantees that concurrent execution gives the same result as serial execution, but deadlocks can still occur
Show answer and explanation
Answer: C. Once a transaction releases any lock, it acquires no new locks afterwards. This guarantees that concurrent execution gives the same result as serial execution, but deadlocks can still occur
Under two-phase locking (2PL), each transaction has two phases, a phase in which it acquires the locks it needs one after another (the growing phase) and a phase in which it releases them (the shrinking phase), and once it starts releasing locks, it acquires no new ones. If every transaction follows this rule, the result of concurrent execution is guaranteed to be the same as executing the transactions one at a time in some order (serializability). However, it cannot prevent a deadlock, in which two transactions each wait for the other to release a lock, so the DBMS detects deadlocks and handles them, for example by rolling back one of the transactions. In practice, strict two-phase locking, which holds exclusive locks until commit, is commonly used; it also prevents other transactions from reading uncommitted data.
- A. Alternating between acquiring and releasing is not two-phase locking. Deadlocks can also occur under two-phase locking.
- B. Without read locks, a transaction can read data that another transaction is in the middle of updating, so dirty reads and other problems are not prevented.
- C. Correct. Acquiring no new locks after releasing begins guarantees serializability, but deadlocks can still occur.