Image for Two-Phase Locking

Two-Phase Locking

Two-Phase Locking (2PL) is a method used in databases to ensure data consistency during concurrent access. It involves two phases: in the first phase (growing phase), a transaction acquires all the necessary locks to access or modify data but doesn't release any locks. In the second phase (shrinking phase), it releases the locks after completing its operations, preventing other transactions from interfering. This approach guarantees serializability, meaning transactions are executed in a way that maintains data integrity as if they were processed one after another. It balances concurrency with data safety, reducing conflicts and errors.