9 Concurrency Control Techniques

Database Management System · Unit 9 · 4 hrs

Concurrency Control Techniques

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

Concurrency Control Techniques in short

This unit covers the concrete techniques a DBMS uses to guarantee the serializability introduced in the last unit: locking protocols, timestamp ordering, multiversion schemes, and optimistic (validation-based) concurrency control, including the snapshot isolation variant widely used in practice.

Concurrency Control Techniques: notes

Two-Phase Locking Technique

A lock is a variable associated with a data item recording the item's status with respect to the operations that may currently be applied to it; conceptually, one lock exists per data item. Two lock modes are standard: shared (S) mode permits an item to be read but not written, and multiple transactions may hold a shared lock on the same item at once, while exclusive (X) mode permits both reading and writing, and only one transaction may hold an exclusive lock on an item at a time.

Requested \ HeldSX
SCompatibleNot compatible
XNot compatibleNot compatible

A transaction that already holds a lock on $X$ may convert it: upgrading from a read lock to a write lock, or downgrading from a write lock to a read lock, and the lock table must then track which transactions hold which locks to support this.

Two-phase locking (2PL) requires every transaction to split its locking behaviour into two phases: a growing phase, in which it may acquire new locks but release none, and a shrinking phase, in which it may release locks but acquire no new ones. The point at which a transaction has acquired every lock it will ever hold is its lock point, marking the boundary between the two phases; if lock conversion is allowed, upgrading must happen in the growing phase and downgrading in the shrinking phase. It is a fundamental theorem of concurrency control that if every transaction in a schedule follows the two-phase locking protocol, the schedule is guaranteed conflict serializable.

Consider a transaction touching items $A$ and $B$ under basic 2PL: $$\text{lock-}X(A),\ \text{read}(A),\ \text{lock-}X(B),\ \text{read}(B),\ \text{write}(A),\ \text{write}(B),\ \text{unlock}(A),\ \text{unlock}(B),\ \text{commit}.$$ The lock point falls right after lock-X(B); everything before it is the growing phase, everything from the first unlock onward is the shrinking phase. Because both unlocks happen before the commit, another transaction could read $A$ or $B$ before this transaction has actually committed, which is exactly the cascading-rollback exposure from the previous unit.

Categories of 2PL trade off this exposure against concurrency.

  • Basic 2PL is the protocol above: correct (guarantees serializability) but not necessarily recoverable on its own.
  • Strict 2PL additionally requires every exclusive lock to be held until after the transaction commits or aborts: $$\text{lock-}X(A),\ \text{read}(A),\ \text{lock-}X(B),\ \text{read}(B),\ \text{write}(A),\ \text{write}(B),\ \text{commit},\ \text{unlock}(A),\ \text{unlock}(B).$$ This guarantees the resulting schedule is both recoverable and cascadeless, at the cost of holding write locks slightly longer, and it is the variant used almost universally in commercial systems.
  • Rigorous 2PL goes further still, holding both exclusive and shared locks until after commit or abort, which is more restrictive than strict 2PL but simplifies implementation.
  • Conservative 2PL requires a transaction to predeclare and acquire every lock it will ever need before it begins executing; if even one needed item cannot be locked immediately, the transaction acquires none of them and waits. This avoids deadlock entirely, at the cost of reduced concurrency, since a transaction may sit idle waiting for every item to become simultaneously available rather than starting on the items it can already lock.

What this unit covers

  • Two-Phase Locking Technique
  • Timestamp Ordering
  • Multiversion Concurrency Control
  • Validation (Optimistic) Techniques and Snapshot Isolation Concurrency Control

Two-Phase Locking Technique

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?[10]

Show solved answer

Two-Phase Locking (2-PL)

Definition

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.

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.

Open this answer on its own page →

2080 · 5 marks

Why do we need concurrency control in databases? Explain. [5]

Concurrency Control is the management procedure required for controlling the execution of operations that take place on a database simultaneously. It is a procedure of managing simultaneous operations without conflicting with each other. --- A DBMS must allow multiple users to access the database...

Open this answer on its own page →

2079 · 5 marks

What is concurrency control? What are its advantages in DBMS? [5]

Concurrency Control is the management procedure in DBMS for managing simultaneous operations without conflicting with each other. It is required for controlling the concurrent execution of operations that take place on a database. A DBMS must allow multiple users to access the database at the sam...

Open this answer on its own page →

2076 · 10 marks

What is concurrency control? Name various methods of controlling the concurrency control? Differentiate between Binary lock and shared/Exclusive lock.[10]

Concurrency Control is the management procedure required for controlling the concurrent execution of operations that take place on a database. It is a procedure of managing simultaneous operations without conflicting with each other. - To apply isolation through mutual exclusion between conflicti...

Open this answer on its own page →

2076 · 5 marks

What is Granularity of data items? How does it effect in concurrency control? [5]

A database is basically represented as a collection of named data items. The size of a data item is called its granularity. A data item can be defined at different levels of size, for example: Granularity Level Example ------ Fine (Small) An individual field (attribute) value of a record Medium A...

Open this answer on its own page →

2076 · 5 marks

Explain 2 phase locking technique in brief. [5]

Locking is an operation which secures permission to read or permission to write a data item. Two Phase Locking (2PL) is a concurrency control protocol that ensures serializability by regulating when transactions may lock and unlock data items. A transaction is said to follow the two-phase locking...

Open this answer on its own page →

2080.1 · 10 marks

Explain deadlock with example. What are different deadlock prevention protocols? Explain deadlock detection and starvation.[10]

Show solved answer

Deadlock: Explanation, Prevention, Detection, and Starvation

1. What is Deadlock?

Deadlock is a condition in a database system (or operating system) where no process proceeds for execution and each process waits for resources that have been acquired by other processes. In other words, a set of transactions are permanently blocked, each waiting for another to release a lock, and none can proceed.

"Deadlock is a condition where no process proceeds for execution and each waits for resources that have been acquired by the other processes."


Example of Deadlock

Consider two transactions T1 and T2, and two data items X and Y:

StepTransaction T1Transaction T2
1Lock(X) -- success
2Lock(Y) -- success
3Lock(Y) -- waits (Y held by T2)
4Lock(X) -- waits (X held by T1)
  • T1 holds X and waits for Y.
  • T2 holds Y and waits for X.
  • Neither can proceed. This is a deadlock.

2. Deadlock Prevention Protocols

Deadlock prevention protocols ensure that the system never enters a deadlock state. Several schemes have been proposed, many using the concept of transaction timestamps.

"A transaction timestamp is a unique identifier assigned to each transaction."

a) Wait-Die Scheme (Non-Preemptive)

  • If transaction Ti requests a resource held by Tj:
    • If Ti is older (smaller timestamp) than Tj, Ti is allowed to wait.
    • If Ti is younger (larger timestamp) than Tj, Ti dies (is rolled back and restarted).
  • Older transactions wait; younger transactions are aborted.

Rule: Wait or Die

If TS(Ti) < TS(Tj)  --> Ti waits
If TS(Ti) > TS(Tj)  --> Ti dies (aborts)

b) Wound-Wait Scheme (Preemptive)

  • If transaction Ti requests a resource held by Tj:
    • If Ti is older than Tj, Ti wounds (preempts/aborts) Tj.
    • If Ti is younger than Tj, Ti waits.
  • Older transactions preempt younger ones; younger transactions wait.

Rule: Wound or Wait

If TS(Ti) < TS(Tj)  --> Ti wounds Tj (Tj aborts)
If TS(Ti) > TS(Tj)  --> Ti waits

c) Timeout-Based Scheme

"If a transaction waits for a period longer than a system-defined timeout period, the system assumes that the transaction may be deadlocked and aborts it -- regardless of whether a deadlock actually exists."

  • Simple and practical due to low overhead.
  • A transaction waiting beyond the timeout limit is automatically aborted and restarted.
  • Limitation: May abort transactions that are not actually deadlocked (false positives).

Comparison Table

SchemeApproachWho Aborts
Wait-DieNon-preemptiveYounger transaction
Wound-WaitPreemptiveYounger transaction (preempted by older)
TimeoutTime-basedTransaction exceeding timeout

3. Deadlock Detection

"An alternative approach to deal with deadlock is deadlock detection, where the system checks if a state of deadlock actually exists."

When deadlock prevention is not used, the system must detect and recover from deadlocks.

How Deadlock Detection Works

The system maintains a Wait-For Graph (WFG):

  • Nodes represent transactions.
  • A directed edge Ti --> Tj means Ti is waiting for a resource held by Tj.
  • A deadlock exists if and only if there is a cycle in the Wait-For Graph.

Example:

T1 --> T2 --> T3 --> T1   (Cycle detected = Deadlock!)

Detection Algorithm Steps

  1. Build the Wait-For Graph from current lock and wait information.
  2. Periodically invoke a cycle-detection algorithm.
  3. If a cycle is found, select a victim transaction (usually the youngest or least-cost transaction).
  4. Abort (rollback) the victim transaction to break the cycle.
  5. Release all locks held by the victim and restart it later.

When to Invoke Detection

  • After every lock request (high overhead, fast detection).
  • Periodically at fixed time intervals (lower overhead, delayed detection).

"This solution is attractive if different transactions will rarely access the same items at the same time."


4. Starvation

"Starvation is a condition where process with higher priorities continuously uses the resources preventing low priority process to acquire the resources."

Starvation occurs when a transaction is indefinitely delayed because other transactions (usually of higher priority) continuously acquire the resources it needs. Unlike deadlock, in starvation some processes do proceed -- just not the starved one.

Difference Between Deadlock and Starvation

AspectDeadlockStarvation
DefinitionNo process proceeds; all wait for each otherSome processes proceed; one is indefinitely delayed
CauseCircular wait among processesPriority-based resource allocation
Processes involvedAll involved processes are blockedOnly the low-priority process is blocked
ResolutionAbort victim, break cycleAging (gradually increase priority)

How Starvation Occurs in Deadlock Recovery

When the system repeatedly selects the same transaction as victim during deadlock recovery, that transaction is continuously rolled back and never completes -- this is starvation.

Prevention of Starvation

  • Aging: Gradually increase the priority of a waiting transaction over time so it eventually
Open this answer on its own page →

Timestamp Ordering

2078 · 10 marks

Explain deadlock and starvation. Explain Time stamp based protocol for concurrency control?[10]

--- Deadlock is a situation in a database system where two or more transactions are waiting indefinitely for each other to release locks on data items, so that none of them can ever proceed. - Transaction T1 holds a lock on item X and waits for a lock on item Y. - Transaction T2 holds a lock on i...

Open this answer on its own page →

2080.1 · 5 marks

How timestamp ordering protocol is used for ensuring concurrency control? [5]

A timestamp is a unique identifier assigned to each transaction by the DBMS, typically based on the system clock or a logical counter at the time the transaction starts. The timestamp ordering protocol uses these timestamps to determine the serialization order of transactions, ensuring concurrenc...

Open this answer on its own page →