BIT202 · 2082 · 10 marks
What is a transaction? Describe how lost update and dirty read problems occur in concurrent execution of transactions? Illustrate with examples.
What is a transaction? Describe how lost update and dirty read problems occur in concurrent execution of transactions? Illustrate with examples.[10]
Quick answer
A transaction is a logical unit of work that consists of a sequence of database operations (such as read and write) that must be executed as a single, indivisible unit. A transaction either completes fully (commits) or has no effect at all (rolls back).
Transaction, Lost Update, and Dirty Read Problems
What is a Transaction?
A transaction must satisfy the ACID properties:
| Property | Description |
|---|---|
| Atomicity | All operations complete successfully or none do |
| Consistency | Database moves from one consistent state to another |
| Isolation | Concurrent transactions do not interfere with each other |
| Durability | Once committed, changes are permanent |
Example of a transaction:
BEGIN TRANSACTION
READ(A)
A = A - 500
WRITE(A)
READ(B)
B = B + 500
WRITE(B)
COMMIT
Problems in Concurrent Execution of Transactions
When multiple transactions execute concurrently without proper control, several problems can arise. Two major problems are:
- Lost Update Problem
- Dirty Read Problem
1. Lost Update Problem
Definition
The lost update problem occurs when two transactions read the same data item and then both update it based on the original value they read. The update made by the first transaction is overwritten (lost) by the second transaction, resulting in incorrect data.
How It Occurs
- Transaction T1 reads a data item X.
- Transaction T2 also reads the same data item X.
- T1 updates X and writes it back.
- T2 updates X based on the old value it read and writes it back.
- T1's update is lost because T2 overwrites it.
Example
Suppose two bank clerks are updating the same account balance simultaneously.
Initial Balance of Account A = 1000
| Time | Transaction T1 | Transaction T2 | Value of A in DB |
|---|---|---|---|
| t1 | READ(A) --> A = 1000 | 1000 | |
| t2 | READ(A) --> A = 1000 | 1000 | |
| t3 | A = A + 200 (A = 1200) | 1000 | |
| t4 | WRITE(A) | 1200 | |
| t5 | A = A - 300 (A = 700) | 1200 | |
| t6 | WRITE(A) | 700 |
Expected Result: 1000 + 200 - 300 = 900
Actual Result: 700
T1's update of +200 is lost because T2 read the old value (1000) before T1 wrote its result, and T2's WRITE overwrote T1's update.
2. Dirty Read Problem
Definition
The dirty read problem (also called the temporary update problem) occurs when a transaction reads data that has been modified by another transaction that has not yet committed. If the first transaction later rolls back, the second transaction has read data that never officially existed, leading to inconsistency.
How It Occurs
- Transaction T1 reads a data item X and updates it.
- Transaction T2 reads the updated (dirty) value of X before T1 commits.
- T1 fails and rolls back, restoring X to its original value.
- T2 has already used the incorrect (dirty) value in its computation.
Example
Suppose a flight booking system where two transactions operate on available seats.
Initial Seats Available = 10
| Time | Transaction T1 | Transaction T2 | Value of Seats in DB |
|---|---|---|---|
| t1 | READ(Seats) --> 10 | 10 | |
| t2 | Seats = 10 - 1 = 9 | 10 | |
| t3 | WRITE(Seats) | 9 (uncommitted) | |
| t4 | READ(Seats) --> 9 | 9 | |
| t5 | ROLLBACK | 10 (restored) | |
| t6 | Seats = 9 - 1 = 8 | ||
| t7 | WRITE(Seats) | 8 |
Expected Result: T1 rolled back, so only T2 should have booked. Seats should be 9.
Actual Result: 8
T2 read the dirty (uncommitted) value of 9 written by T1. After T1 rolled back, the actual value was restored to 10, but T2 already computed based on 9, causing an incorrect final value of 8.
Summary Comparison
| Problem | Cause | Effect |
|---|---|---|
| Lost Update | Two transactions read and write the same item concurrently | One transaction's update is overwritten and lost |
| Dirty Read | A transaction reads uncommitted data of another transaction | Incorrect data is used if the writing transaction rolls back |
Solution
These problems are solved by concurrency control mechanisms such as:
- Locking protocols (shared lock, exclusive lock)
- Timestamp ordering
- Serializable schedules
These ensure that concurrent transactions produce results equivalent to some serial execution of those transactions.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.