Learn how shared and exclusive locks are used to control access to data.
Lock-based protocols are the most common concurrency control mechanism used in database systems. The fundamental idea is that a transaction must acquire a 'lock' on a data item before it can perform an operation on it. This lock prevents other transactions from performing conflicting operations on the same data item. There are two primary types of locks. A Shared Lock (S-lock), also known as a read lock, is required for a transaction to read a data item. Multiple transactions can hold a shared lock on the same item simultaneously because reading data does not cause conflicts. An Exclusive Lock (X-lock), also known as a write lock, is required for a transaction to write (modify or delete) a data item. An exclusive lock is exclusive; if one transaction holds an X-lock on an item, no other transaction can acquire any lock (neither shared nor exclusive) on that same item. This ensures that a transaction has exclusive access while it is modifying data, preventing issues like lost updates and dirty reads. To guarantee serializability, protocols like Two-Phase Locking (2PL) are used. In 2PL, a transaction has two phases: a 'growing phase' where it can only acquire locks, and a 'shrinking phase' where it can only release locks. This protocol ensures that transactions behave in a serializable manner, though it can still lead to deadlocks.