1. Gyanastra
  2. Database Systems
  3. Concurrency Control (Locking)
  4. Lock-Based Protocols and Shared vs Exclusive Locks
Free lesson, no account needed 5 min read 8 practice questions after it
Asked in past papers3 times
  • GATE CSE2004
  • ISRO2009
  • UGC NET2012

Database Systems · Concurrency Control (Locking)

Lock-Based Protocols and Shared vs Exclusive Locks

Overview

Lock-based protocols are the mechanism by which a transaction must acquire a lock on a data item before it reads or writes it.
Because there are two operations (read and write), two lock modes are needed: shared (read) locks and exclusive (write) locks.
Their compatibility rules enforce mutual exclusion so that concurrent transactions cannot produce inconsistencies such as race conditions.

Key concepts

  • Lock-based protocol: a transaction must lock a data item before accessing it. To read A, first lock A, then read A; to write A, first lock A, then write A.
  • Shared lock (S): used when a transaction wants to read a data item; also called a read lock. Notation: LOCK_R(A) — apply a read lock on data item A.
  • Exclusive lock (X): used when a transaction wants to write a data item; also called a write lock. While it is held, no other transaction can read or write that data item. Notation: LOCK_W(A) — apply a write lock on data item A.
  • Mutual exclusion: the same idea as in operating systems, but here there are two operations, read and write, so two lock modes are used.
  • Notation varies: lock notation differs from textbook to textbook; an exam will state the meaning clearly, e.g. LOCK_R(A) means locking A for reading and LOCK_W(A) means locking A for writing.

How it works

  1. A transaction that wants to read data item A takes a shared lock: LOCK_R(A), then reads A.
  2. A transaction that wants to write data item A takes an exclusive lock: LOCK_W(A), then writes A.
  3. If A already has a shared lock, another transaction can also get a shared lock on it.
  4. If A already has a shared lock, another transaction cannot get an exclusive lock on it.
  5. If A already has an exclusive lock, another transaction cannot get a shared lock on it.
  6. If A already has an exclusive lock, another transaction cannot get an exclusive lock on it.
Figure. A read takes LOCK_R A and a write takes LOCK_W A before access. A shared lock is granted alongside another shared lock, but an exclusive request is denied while any lock is held.

Formulas

  • LOCK_R(A) — apply a read (shared) lock on data item A.

  • LOCK_W(A) — apply a write (exclusive) lock on data item A.

  • S = shared lock; X = exclusive lock.

  • Lock Compatibility Decision

  • Shows how a requested shared or exclusive lock is granted or denied depending on the lock already held on the data item.

Worked examples

Accessing data item A:

  1. To read A: take a shared/read lock on A with LOCK_R(A), then read A.
  2. To write A: take an exclusive/write lock on A with LOCK_W(A), then write A.
  3. If A is already shared-locked for reading: another read request can also get a shared lock, but a write request cannot get an exclusive lock.
  4. If A is already exclusively locked for writing: no other transaction can get either a shared lock or an exclusive lock on A.

Comparison

Lock already held on data itemRequested shared lock (S)Requested exclusive lock (X)
Shared lock (S)AllowedNot allowed
Exclusive lock (X)Not allowedNot allowed

Revision summary

  • A lock must be taken before the access, not after: lock A, then read or write A.
  • Shared lock = read lock (LOCK_R(A)); exclusive lock = write lock (LOCK_W(A)).
  • Any number of transactions can read the same data item at the same time.
  • While someone is reading, no one should update the data item; a shared lock blocks an exclusive lock.
  • While someone is updating, no one should read the data item; an exclusive lock blocks both shared and exclusive locks.
  • Without these rules, inconsistencies such as race conditions can occur.
  • Exam trap: lock notation may differ between textbooks — the exam will state what each lock command means.

Previous-year exam insights

  • Lock compatibility matrix: S is compatible with S; S is incompatible with X; X is incompatible with both S and X. A transaction holding an S lock can upgrade to X only if no other transaction holds an S lock on that item.
    Asked inGATE 2004UGC NET 2012
  • Lock granularity is the size of the data item on which a lock is placed (field, record, page, table, file, database).
    Finer granularity gives higher concurrency but more locking overhead; coarser granularity gives less concurrency but lower overhead.
    Asked inGATE 2004UGC NET 2012
  • A record lock prevents two users from updating the same record simultaneously; the second user's update request must wait until the first releases the lock.
    Asked inUGC NET 2012
  • A file lock (or any exclusive lock) allows only one transaction to access the locked item at a time; other transactions are blocked until the lock is released.
    Asked inISRO 2009
  • Locking alone does not guarantee serializability: a protocol such as two-phase locking (growing phase acquires locks, shrinking phase releases them) is required to ensure conflict serializability.
  • A transaction that holds a lock must release it (UNLOCK) after its last access; locks are released either at commit/abort or according to the protocol's rules.
Pages 1–2 of 2