To guarantee that concurrent transactions do not corrupt the database, the DBMS must ensure that the schedule of operations is Serializable (meaning the interleaved execution is mathematically equivalent to some sequential execution).
Simply acquiring and releasing locks is not enough. If a transaction acquires a lock, reads data, releases the lock, and then acquires another lock, it can still suffer from inconsistencies. To guarantee serializability, transactions must follow a specific protocol. The most widely used is the Two-Phase Locking Protocol (2PL).
The 2PL protocol dictates that every transaction must issue lock and unlock requests in exactly two distinct phases:
The Mathematical Guarantee: It has been mathematically proven that if all transactions in a system strictly obey the 2PL rules, the resulting schedule is guaranteed to be conflict-serializable. The database will always remain consistent.
While basic 2PL guarantees serializability, it has a major flaw: Cascading Rollbacks.
If transaction T1 modifies data, releases the lock (entering shrinking phase), and T2 immediately acquires the lock and reads the data. If T1 later fails and aborts, the data T2 read is now invalid. T2 must also be aborted. This creates a chain reaction.
To solve this, modern databases use stricter variations of 2PL:
It is extremely important to note that 2PL does not prevent deadlocks. Because transactions are allowed to hold locks while waiting for new ones during the Growing Phase, circular wait conditions can easily occur.
Therefore, any DBMS utilizing 2PL must also implement a deadlock detection subsystem to automatically abort deadlocked transactions.