22.6 KB · commit 40bb182
← Previous: T09.4: Concurrent Execution and Schedules | 🏠 Index | Next: T10.1: Conflicting Instructions and Conflict Serializability →
Topic: T09: Transactions, ACID & Anomalies | Priority: 🟡 Medium Priority | Exam Frequency: 2/7 finals (+ CT-04) (7 marks)
Sources: Silberschatz 7e Ch. 17 (§17.5) · Lecture Deck 07 (Slides 28–33) · Classnote by Raidah (T09, pp. 26, 28)
Running transactions concurrently keeps the CPU and disks busy, but if the DBMS lets transactions interleave without any rules, they step on each other's toes and break Isolation. This causes classic data corruption bugs known as concurrency anomalies. RUET exams regularly ask you to list what goes wrong when concurrency control is missing and prove your point with a step-by-step schedule table.
Concurrency-Control Component: The concurrency-control component is the DBMS subsystem that coordinates simultaneous transactions. It enforces isolation using locks or timestamps so that concurrent schedules always leave the database in a consistent state.
Lost Update: A lost update occurs when two concurrent transactions update the same data item and the second write overwrites the first write. As a result, the modification made by the first transaction is wiped out.
Dirty Read (Temporary Update): A dirty read occurs when one transaction updates a data item and a second transaction reads that uncommitted value before the first transaction fails and rolls back. The second transaction ends up using temporary data that was never committed.
Unrepeatable Read: An unrepeatable read occurs when a transaction reads the same data item twice during its execution and gets two different values because another transaction modified and committed that item in between the two reads.
Phantom Read: A phantom read occurs when a transaction reads a data item or row set once, and another concurrent transaction deletes that item or inserts new matching rows before the second read. In the RUET course slides, it specifically refers to a second read failing with a "variable does not exist" error after another transaction deletes the variable.
Imagine you and a teammate both open a shared inventory spreadsheet showing 20 laptops in stock. You sell 2 laptops and save "18", while your teammate receives 5 new laptops and saves "25" a second later. Because your teammate's save overwrites yours, the 2 laptops you sold vanish from the record (Lost Update). Or suppose your teammate types "1,000" by mistake, you copy that number into a financial report, and then your teammate hits Undo (Dirty Read). Every concurrency anomaly is just a database version of two people reading and writing the same shared data without coordinating.
When transaction isolation fails, five classic concurrency anomalies can corrupt the database:
| Anomaly Name | Alternative Name | What Causes It | What Goes Wrong |
|---|---|---|---|
| Lost Update | Overwrite Problem | A second transaction overwrites a value written by the first transaction | The first transaction's update disappears completely |
| Dirty Read | Temporary Update | A transaction reads an uncommitted update from another transaction that later aborts | Calculations are based on phantom numbers that were rolled back |
| Unrepeatable Read | Fuzzy Read | Another transaction modifies and commits an item between two reads in the same transaction | Reading the same variable twice inside one transaction gives two different answers |
| Phantom Read | Missing / Appearing Item | Another transaction deletes a variable (or inserts new matching rows) between two reads | The second read fails because the variable is gone (or sees brand-new rows) |
| Incorrect Summary | Inconsistent Analysis | A summary query (SUM / AVG) runs while a transfer is halfway across accounts | The total either misses money in transit or counts it twice |
A lost update happens when two concurrent transactions update the same data item and the second write blindly overwrites the first write. As a result, the first transaction's change is wiped out as if it never happened.
Suppose T1 deducts 4 items from stock S=20 and writes S=16. Before or right after T1 commits, T2 performs a write setting S=30 (either from an old read of 20 plus 10, or a direct blind write). Once both commit, the database stores S=30, and T1's deduction of 4 items is completely lost. (See the full exam table with A=500 in GQ1 below).
A dirty read happens when transaction T1 updates a data item, and transaction T2 reads that new value before T1 commits. We call uncommitted data "dirty" because it is not final yet. If T1 crashes or aborts a moment later, the recovery system rolls T1's write back, leaving T2 holding a value that never officially existed.
An unrepeatable read happens when a single transaction T1 reads the same data item twice during its run, but gets two different values because another transaction T2 modified and committed that item in between the two reads.
| Time | Transaction T1 | Transaction T2 | Database State / What Happens |
|---|---|---|---|
| t1 | read(A) | T1 reads A=500 | |
| t2 | read(A) | T2 also reads A=500 | |
| t3 | A:=A+100 | T2 calculates A=600 locally | |
| t4 | write(A) | T2 updates database to A=600 | |
| t5 | commit | T2 commits its change permanently | |
| t6 | read(A) | T1 reads A a second time and gets 600 instead of 500! |
Result: Inside a single transaction, T1 asked for the value of A twice and got two contradictory answers (500 vs. 600).
In the RUET lecture slides (Deck 07, Slide 32), the phantom read problem is taught as a concurrent deletion error: transaction T2 reads variable A once, then transaction T1 deletes A, and when T2 tries to read A a second time, the database throws an error saying A no longer exists.
| Time | Transaction T1 | Transaction T2 | Database State / What Happens |
|---|---|---|---|
| t1 | read(A) | read(A) | Both transactions read data item A successfully |
| t2 | delete(A) | T1 deletes data item A from the database | |
| t3 | read(A) | T2 tries to read A again, triggering a "variable does not exist" error |
Note: In standard SQL and Silberschatz textbook terminology, a phantom read also refers to running a
WHERErange query twice in one transaction and seeing brand-new ("phantom") rows appear on the second run because another transaction inserted rows in the meantime. In RUET exams, always give the Slide 32 deletion table above first, and you can mention the textbook row-insertion meaning as a bonus note.
The incorrect summary problem happens when an aggregate transaction T2 calculates a SUM or AVG across several accounts while another transaction T1 is transferring money between those accounts. If T2 reads one account after money is deducted and reads the other account before the money is added, its total will come up short.
| Time | Transaction T1 (Transfer 50 from A to B) | Transaction T2 (Average of A,B,C) | Running Total in T2 |
|---|---|---|---|
| t1 | read(A);A:=A−50;write(A); | T1 reduces A to 950 on disk | |
| t2 | sum:=0;read(C);sum:=sum+C; | T2 reads C=1000⟹sum=1000 | |
| t3 | read(A);sum:=sum+A; | T2 reads updated A=950⟹sum=1950 | |
| t4 | read(B);sum:=sum+B; | T2 reads old B=1000⟹sum=2950 | |
| t5 | avg:=sum/3;commit; | T2 reports avg=2950/3≈983.33 | |
| t6 | read(B);B:=B+50;write(B);commit; | T1 finishes adding 50 to B (B=1050) |
Result: The true total across accounts A,B, and C is 3000 (an average of 1000). Because T2 caught T1 halfway through its transfer, 50 was missed and T2 reported an inconsistent average of 983.33.
| Year & Question | Marks | Question Summary | Required Content |
|---|---|---|---|
| 2018 Q.5(a) | 4 | Concurrency problems if control is omitted | List the 5 problems and explain Lost Update and Dirty Read with schedule tables |
| 2024 Q.8(a) | 3 | Concurrency problems if control is omitted | Exact repeat of 2018 Q.5(a). List the problems and show concrete schedule tables |
| CT-04 Q.03 | 4 | Explain any two problems caused by concurrency | Explain Dirty Read and Lost Update with full step-by-step schedules |
Appeared: 2018 Q.5(a) (4 marks), 2024 Q.8(a) (3 marks)
Question:
If concurrency-control management is not implemented properly what problem may occur? Explain with example.
Full Answer:
If concurrency-control management is not implemented properly, concurrent transactions interleave without restriction and violate the ACID property of Isolation, leaving the database in an inconsistent, corrupted state.
The five major concurrency problems that can occur are:
Below are two detailed examples with step-by-step schedules showing how data corruption happens:
In the lost update problem, an update made to a data item by one transaction is lost because a second concurrent transaction overwrites that data item without accounting for the first update.
Initial State: Account A=500.
| Time | Transaction T1 | Transaction T2 | Database State / Description |
|---|---|---|---|
| t1 | read(A) | T1 reads A=500 | |
| t2 | A:=A−50 | T1 computes new balance 450 locally | |
| t3 | write(A) | Database is updated to A=450 | |
| t4 | write(A) | T2 performs a blind write setting A:=25 | |
| t5 | commit | T1 commits | |
| t6 | commit | T2 commits, leaving final database value A=25 |
Impact: The deduction of 50 made by T1 is completely overwritten and lost.
In the dirty read problem, one transaction updates a data item and a second transaction reads that uncommitted ("dirty") value before the first transaction finishes. When the first transaction later fails and rolls back, the second transaction has calculated and saved results based on a temporary number that was cancelled.
Initial State: Account A=100.
| Time | Transaction T1 | Transaction T2 | Database State / Description |
|---|---|---|---|
| t1 | read(A) | T1 reads initial balance A=100 | |
| t2 | A:=A+100 | T1 computes A=200 locally | |
| t3 | write(A) | Database is updated to A=200 (uncommitted) | |
| t4 | read(A) | T2 reads the uncommitted dirty value A=200 | |
| t5 | A:=A−50 | T2 computes A=150 locally | |
| t6 | write(A) | Database is updated to A=150 | |
| t7 | commit | T2 commits its update based on dirty data | |
| t8 | Failure / Rollback | T1 fails and aborts (its +100 change is cancelled) |
Impact: T2 committed A=150 based on a temporary value of 200 that was rolled back, corrupting the database.
Appeared: CT-04 Q.03 (4 marks)
Question:
Explains any two problems caused by concurrent execution. (Note: Printed verbatim in original script).
Full Answer:
Two major problems caused by uncontrolled concurrent execution are the Dirty Read Problem and the Lost Update Problem:
| Step | Transaction T1 | Transaction T2 | Action |
|---|---|---|---|
| 1 | read(A) | T1 reads A=100 | |
| 2 | A:=A+100;write(A) | T1 writes uncommitted value A=200 | |
| 3 | read(A) | T2 reads dirty value A=200 | |
| 4 | A:=A−50;write(A) | T2 writes A=150 | |
| 5 | commit | T2 commits permanently | |
| 6 | rollback | T1 fails and rolls back |
Because T2 used T1's uncommitted value of 200 before T1 aborted, the committed balance A=150 is wrong.
| Step | Transaction T1 | Transaction T2 | Action |
|---|---|---|---|
| 1 | read(A) | T1 reads A=500 | |
| 2 | A:=A−50;write(A) | T1 updates A to 450 | |
| 3 | write(A) | T2 overwrites A with a blind write A=25 | |
| 4 | commit | T1 commits | |
| 5 | commit | T2 commits, leaving A=25 in the database |
The reduction of 50 performed by T1 is overwritten and permanently lost.
Source: Silberschatz 7e · Chapter 18 (Exercise 18.31)
Exam Relevance: High-Probability Exam Drill (Interleaved Schedules)
Key Tested Concept: Concrete step-by-step trace of Lost Update and Dirty Read (Uncommitted Dependency) anomalies.
Problem Statement:
- Construct an interleaved execution schedule of two transactions T1 and T2 that demonstrates the Lost Update anomaly.
- Construct an interleaved execution schedule demonstrating the Dirty Read anomaly.
- Explain how the Two-Phase Locking (2PL) protocol prevents both phenomena.
Model Exam-Style Solution:
| Step | Transaction T1 | Transaction T2 | Database State / Result |
|---|---|---|---|
| 1 | read(A) [A=100] | Local A1=100 | |
| 2 | read(A) [A=100] | Local A2=100 | |
| 3 | A:=A−20 | Local A1=80 | |
| 4 | write(A) [A=80] | Disk A=80 | |
| 5 | A:=A+30 | Local A2=130 | |
| 6 | write(A) [A=130] | Disk A=130 (T1's update is LOST!) | |
| 7 | commit | commit | Final A=130 instead of 110 |
| Step | Transaction T1 | Transaction T2 | Database State / Result |
|---|---|---|---|
| 1 | read(A) [A=100] | Local A1=100 | |
| 2 | A:=A+100 | Local A1=200 | |
| 3 | write(A) [A=200] | Disk A=200 (Uncommitted!) | |
| 4 | read(A) [A=200] | T2 reads uncommitted "dirty" value | |
| 5 | write(A) or print A | T2 bases decision on dirty value | |
| 6 | abort (Rollback) | Disk A restored to 100 | |
| 7 | commit | T2 committed based on a value that never legally existed! |
💡 RUET Exam Takeaway: In tabular schedules, clearly write out the local variable values and annotate the exact moment an update is overwritten or an uncommitted value is read.
Source: Silberschatz 7e · Chapter 18 (Exercise 18.32)
Exam Relevance: Prime Overdue Advanced Concurrency Question
Key Tested Concept: Phantom tuples, why standard tuple-level 2PL fails, and predicate/index locking solutions.
Problem Statement:
- Explain the Phantom Phenomenon with a concrete banking database example.
- Explain why standard tuple-level Two-Phase Locking (2PL) cannot prevent phantom tuples.
- Describe how Index-Locking or Predicate Locking resolves this problem.
Model Exam-Style Solution:
account(account_number, branch_name, balance).'Perryridge' branch:SELECT SUM(balance) FROM account WHERE branch_name = 'Perryridge';
'Perryridge' with balance 500:INSERT INTO account VALUES ('A-301', 'Perryridge', 500);
'Perryridge' (say A-101 and A-102), reading total=1200.('A-301', 'Perryridge', 500) and commits.A-301 appears unexpectedly as a "phantom"!Why Standard Tuple-Level 2PL Fails:
'A-301' does not exist in the database yet.'A-301' does not exist, T1 cannot place a lock on it!'A-301' without requesting a lock on an existing item that would conflict with T1. The conflict is on the predicate (branch_name = 'Perryridge'), not on a physical data item.Solutions:
branch_name = 'Perryridge'). Any transaction attempting to insert or modify a tuple satisfying the predicate must conflict. However, predicate locking has high computational overhead (NP-hard in general).branch_name, T1 acquires a lock on the index leaf node or the search-key bucket for 'Perryridge'.'A-301', it must insert an entry into the 'Perryridge' index bucket.'Perryridge', T2's insertion is blocked until T1 commits. ■💡 RUET Exam Takeaway: Phantoms occur because locks can only be placed on data items that exist. To prevent phantoms, DBMSs lock the index entries that cover the query search space.
← Previous: T09.4: Concurrent Execution and Schedules | 🏠 Index | Next: T10.1: Conflicting Instructions and Conflict Serializability →