Locking Strategies

Learn how to distinguish the differences between different types of locking strategies.

A lock on a database item ensures that only one transaction updates that item in the presence of concurrent transactions. A lock ensures that the database remains consistent before and after the transaction. We’ll discuss three different types of locking strategies in this lesson:

  • Two-phase locking

  • Predicate locking

  • Index range locking

Two-phase locking

Two-phase locking (2PL) is a type of concurrency control that provides a serializability isolation level. In two-phase locking, multiple transactions are allowed to read concurrently if no transaction wants to write the data. When a transaction wants to write the data, it takes an exclusive lock and blocks all readers and writers.

In a two-phase locking:

  • Writers block other writers and readers.

  • Readers don't block other readers.

Two-phase locking blocks readers and writers by taking a lock in the database. There are two types of locks:

  • Shared lock: A lock acquired by readers that allows concurrent transactions to read data independently without blocking each other.

  • Exclusive lock: A lock acquired by a writer that blocks all other readers and ...