2080 3

CSC265 ยท 2080 ยท 10 marks

What is two-phase locking? What are different types of locks in two-phase locking? Explain basic, conservative, strict, rigorous two-phase locking. What is lock conversion?

What is two-phase locking? What are different types of locks in two-phase locking? Explain basic, conservative, strict, rigorous two-phase locking. What is lock conversion?[10]

Database Management SystemChapter 9: Concurrency Control TechniquesTwo-Phase Locking Technique

Quick answer

A transaction is said to follow the two-phase locking (2-PL) protocol if all locking operations (read-lock, write-lock) precede the first unlock operation in the transaction. Locking is an operation which secures permission to read or permission to write a data item. Two-phase locking is a process used to gain ownership of shared resources in a way that guarantees conflict serializable schedules. Note that basic two-phase locking does not by itself remove the possibility of deadlock: two transactions can each hold a lock the other is waiting for. Only the conservative variant, which acquires every lock before execution begins, is deadlock free.

Two-Phase Locking (2-PL)

Definition

Such a transaction is divided into exactly two phases:

PhaseNameDescription
Phase 1Growing (Expanding) PhaseNew locks on items can be acquired but none can be released. The point when a transaction acquires all the locks it needs is called the Lock Point.
Phase 2Shrinking PhaseExisting locks can be released but no new locks can be acquired.

The three activities in the two-phase update algorithm are:

  1. Lock Acquisition
  2. Modification of Data
  3. Release of Lock

Types of Locks in Two-Phase Locking

The two main modes in which a data item may be locked are:

  1. Exclusive (X) Mode - The data item can be both read and written. Only one transaction can hold an exclusive lock on an item at a time.
  2. Shared (S) Mode - The data item can only be read. Multiple transactions can hold shared locks on the same item simultaneously.

Categories of Two-Phase Locking

1. Basic Two-Phase Locking (Basic 2-PL)

  • A transaction follows the two-phase protocol: it has a growing phase where locks are acquired and a shrinking phase where locks are released.
  • Once the transaction releases even one lock, it enters the shrinking phase and cannot acquire any new locks.
  • Problem: It does not prevent dirty reads or cascading rollbacks, because locks may be released before the transaction commits.
|-- Growing Phase --|-- Shrinking Phase --|
  Acquire locks         Release locks
       ^
    Lock Point

2. Conservative Two-Phase Locking (Static 2-PL)

  • A transaction must lock all the data items it needs before it begins execution (i.e., before any read or write operation is performed).
  • If any required lock cannot be obtained, the transaction does not lock any item at all and waits.
  • Advantage: Prevents deadlock because all resources are acquired upfront.
  • Disadvantage: It is difficult to know in advance all the data items that will be needed; this reduces concurrency.

3. Strict Two-Phase Locking (Strict 2-PL)

  • In addition to following basic 2-PL, a transaction must hold all its Exclusive (X) locks until after it commits or aborts.
  • Shared (S) locks may be released before the transaction commits.
  • Advantage: Prevents dirty reads and cascading rollbacks because no other transaction can read or write an item that has been exclusively locked until the holding transaction commits.
  • Advantage: Ensures strict schedules, which are easier to recover from.

4. Rigorous Two-Phase Locking (Rigorous 2-PL)

  • This requires, in addition to basic 2-PL, that all Exclusive (X) and Shared (S) locks held by the transaction be released only after the transaction commits or aborts.
  • The key difference between Strict 2-PL and Rigorous 2-PL:
    • Strict 2-PL holds only X locks until commit.
    • Rigorous 2-PL holds both X and S locks until commit, making it more restrictive.
  • Advantage: Produces serializable and easily recoverable schedules.
  • Disadvantage: Lower concurrency because locks are held for a longer duration.

Summary Comparison Table

ProtocolHolds X locks until commit?Holds S locks until commit?Prevents Deadlock?
Basic 2-PLNoNoNo
Conservative 2-PLYes (pre-acquired)Yes (pre-acquired)Yes
Strict 2-PLYesNoNo
Rigorous 2-PLYesYesNo

Lock Conversion

When lock conversion is allowed in two-phase locking, a transaction can change the mode of a lock it already holds on a data item. The rules are:

  • Upgrading (S lock โ†’ X lock): Upgrading of a lock from Shared to Exclusive must be done during the growing (expanding) phase only.
  • Downgrading (X lock โ†’ S lock): Downgrading of a lock from Exclusive to Shared must be done during the shrinking phase only.

This ensures the two-phase property is still maintained while allowing more flexibility in lock management.

Example: A transaction first reads a data item (acquires S lock), and later decides to write it. It can upgrade the S lock to an X lock during the growing phase. When it no longer needs to write but still needs to read, it can downgrade the X lock to an S lock during the shrinking phase.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.