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.
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 answerHide 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:
| Phase | Name | Description |
|---|---|---|
| Phase 1 | Growing (Expanding) Phase | New 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 2 | Shrinking Phase | Existing locks can be released but no new locks can be acquired. |
The three activities in the two-phase update algorithm are:
- Lock Acquisition
- Modification of Data
- Release of Lock
Types of Locks in Two-Phase Locking
The two main modes in which a data item may be locked are:
- 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.
- 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
| Protocol | Holds X locks until commit? | Holds S locks until commit? | Prevents Deadlock? |
|---|---|---|---|
| Basic 2-PL | No | No | No |
| Conservative 2-PL | Yes (pre-acquired) | Yes (pre-acquired) | Yes |
| Strict 2-PL | Yes | No | No |
| Rigorous 2-PL | Yes | Yes | No |
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.
2080 · 5 marks
Why do we need concurrency control in databases? Explain. [5]
2079 · 5 marks
What is concurrency control? What are its advantages in DBMS? [5]
2076 · 10 marks
What is concurrency control? Name various methods of controlling the concurrency control? Differentiate between Binary lock and shared/Exclusive lock.[10]
2076 · 5 marks
What is Granularity of data items? How does it effect in concurrency control? [5]
2080.1 · 10 marks
Explain deadlock with example. What are different deadlock prevention protocols? Explain deadlock detection and starvation.[10]
Show solved answerHide 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:
| Step | Transaction T1 | Transaction T2 |
|---|---|---|
| 1 | Lock(X) -- success | |
| 2 | Lock(Y) -- success | |
| 3 | Lock(Y) -- waits (Y held by T2) | |
| 4 | Lock(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
| Scheme | Approach | Who Aborts |
|---|---|---|
| Wait-Die | Non-preemptive | Younger transaction |
| Wound-Wait | Preemptive | Younger transaction (preempted by older) |
| Timeout | Time-based | Transaction 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
- Build the Wait-For Graph from current lock and wait information.
- Periodically invoke a cycle-detection algorithm.
- If a cycle is found, select a victim transaction (usually the youngest or least-cost transaction).
- Abort (rollback) the victim transaction to break the cycle.
- 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
| Aspect | Deadlock | Starvation |
|---|---|---|
| Definition | No process proceeds; all wait for each other | Some processes proceed; one is indefinitely delayed |
| Cause | Circular wait among processes | Priority-based resource allocation |
| Processes involved | All involved processes are blocked | Only the low-priority process is blocked |
| Resolution | Abort victim, break cycle | Aging (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
Timestamp Ordering
2078 · 10 marks
Explain deadlock and starvation. Explain Time stamp based protocol for concurrency control?[10]
2080.1 · 5 marks
How timestamp ordering protocol is used for ensuring concurrency control? [5]
Make Unit 9 stick
Practice CSC265 with flashcards & quizzes