8 Concurrency Control

Database Management System · Unit 8

Concurrency Control

Exam-focused notes for Concurrency Control (Database Management System, BIT202): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.

Concurrency Control in short

Many users use a database at the same time. Concurrency control is the part of the DBMS that lets them work together without damaging the data. This chapter covers locks, two-phase locking, deadlock, and starvation.

Concurrency Control: notes

Concurrent execution in multiuser systems

A multiuser DBMS lets many users run transactions at the same time. The DBMS interleaves their operations, so while one transaction waits for the disk, another can use the CPU.

Benefits of concurrent execution:

  • Higher throughput, which means more transactions finish per second.
  • Better use of CPU and disk.
  • Shorter waiting time, especially for short transactions behind long ones.

The risk is that uncontrolled interleaving breaks the isolation property. The concurrency control manager (scheduler) must make every interleaved schedule equal to some serial schedule.

Need for concurrency control

Without control, transactions can cause the following problems.

  • Lost update: one transaction overwrites another's write.
  • Dirty read: a transaction reads data that is later rolled back.
  • Unrepeatable read: a transaction reads the same item twice and gets different values, because another transaction changed it between the reads.
  • Incorrect summary: a total is calculated while the data is being changed.

Example: two clerks sell the last ticket at the same time. Both read "1 seat left" and both book it. One seat is sold twice. Locking makes the second clerk wait until the first has finished.

The main techniques are locking, timestamp ordering, and optimistic methods. Locking is the most common one.

Binary locks

A lock is a variable linked to a data item. It controls whether other transactions may access that item. A binary lock has only two states: locked (1) and unlocked (0).

Operations:

  • lock(X): if X is unlocked, lock it. If it is locked, the transaction waits.
  • unlock(X): release the lock, and wake up a waiting transaction.

Rules for a transaction:

  • It must lock X before reading or writing X.
  • It must unlock X after it has finished with X.
  • It must not lock an item it already holds.
  • It must not unlock an item it does not hold.

Binary locks are simple but too strict. Even two transactions that only read X cannot run together, which wastes concurrency.

What this unit covers

  • Concurrent execution in multiuser systems
  • Need for concurrency control
  • Binary locks
  • Shared and exclusive locks
  • Two-phase locking protocol
  • Deadlock definition and examples
  • Starvation in DBMS

Two-phase locking protocol

2082 · 5 marks

What is two phase locking protocol? How can it lead to the deadlock condition? [5]

Show solved answer

Two Phase Locking (2PL) Protocol

Definition

Two Phase Locking (2PL) is a concurrency control protocol that ensures serializability of transactions by regulating when locks can be acquired and released. It states that:

A transaction must acquire all the locks it needs before releasing any lock.

The protocol divides the execution of a transaction into two distinct phases:


The Two Phases

Phase 1: Growing Phase (Lock Acquisition Phase)

  • A transaction may acquire locks (shared or exclusive).
  • A transaction may NOT release any lock.
  • The number of locks held by the transaction increases (or stays the same).

Phase 2: Shrinking Phase (Lock Release Phase)

  • A transaction may release locks.
  • A transaction may NOT acquire any new lock.
  • The number of locks held decreases until all are released.

The point at which the last lock is acquired is called the Lock Point.

Number
of Locks
    |        /\
    |       /  \
    |      /    \
    |     /      \
    |    /        \
    |___/          \___
    |
    +--Growing--+--Shrinking-->  Time

Types of 2PL

TypeDescription
Basic 2PLLocks released during shrinking phase
Strict 2PLAll exclusive locks held until commit/abort
Rigorous 2PLAll locks held until commit/abort

How 2PL Can Lead to Deadlock

Deadlock occurs when two or more transactions are waiting for each other to release locks, forming a circular wait condition.

Example:

Consider two transactions T1 and T2:

StepT1T2
1Lock-X(A) -- success
2Lock-X(B) -- success
3Lock-X(B) -- WAIT (B locked by T2)
4Lock-X(A) -- WAIT (A locked by T1)

Result:

  • T1 holds lock on A and waits for B
  • T2 holds lock on B and waits for A
  • Neither can proceed -- DEADLOCK

Why 2PL Causes This:

In the growing phase, a transaction acquires locks one at a time (not all at once). This means:

  • T1 acquires lock on A, then tries to acquire lock on B
  • T2 acquires lock on B, then tries to acquire lock on A
  • Since neither can release a lock during the growing phase (2PL rule), a circular dependency forms

This is a direct consequence of the 2PL rule that locks cannot be released until the shrinking phase begins, combined with the fact that locks are acquired incrementally.


Deadlock Handling

Since 2PL can cause deadlocks, the DBMS must handle them using:

  • Deadlock Detection -- using a wait-for graph; if a cycle is detected, one transaction is rolled back (victim selection)
  • Deadlock Prevention -- using timestamp-based schemes (Wait-Die or Wound-Wait protocols)

Summary

FeatureDetail
PurposeEnsures serializability
Phase 1Acquire locks only (Growing)
Phase 2Release locks only (Shrinking)
DeadlockPossible due to incremental lock acquisition and circular wait
Open this answer on its own page →

Shared and exclusive locks

2080 · 5 marks

What is Shared/Exclusive (Read/Write) Locks? How it is different from binary Locks. [5]

Note: Reference notes were not available for this topic. The following answer is based on standard database concurrency control concepts as taught in BSc CSIT curriculum. --- A binary lock has only two states: State Value Meaning ----------------------- Locked 1 Some transaction is using the data...

Open this answer on its own page →

Deadlock definition and examples

2080 · 5 marks

What is Deadlock in DBMS? Explain with example. [5]

A deadlock is a situation in a database system where two or more transactions are waiting indefinitely for each other to release locks, such that none of them can ever proceed. Each transaction holds a resource that another transaction needs, creating a circular wait condition. --- For a deadlock...

Open this answer on its own page →

Need for concurrency control

2079 · 5 marks

What is meant by the concurrent execution of database transactions in a multiuser system? Discuss why concurrency control is needed, and give informal examples. [5]

In a multiuser database system, multiple transactions are submitted and executed at the same time (overlapping in time) rather than one after another. The database system interleaves the operations of multiple transactions to improve: - CPU utilization (while one transaction waits for I/O, anothe...

Open this answer on its own page →

Starvation in DBMS

2078 · 5 marks

What is starvation in DBMS? Explain with example. [5]

Starvation (also called indefinite blocking) is a situation in which a transaction waits indefinitely for a lock or resource because other transactions are continuously granted priority over it, preventing it from ever proceeding. In other words, a transaction is said to be starved when it is rep...

Open this answer on its own page →