2082 1

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]

Database Management SystemChapter 7: Transaction ManagementLost update problem

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:

PropertyDescription
AtomicityAll operations complete successfully or none do
ConsistencyDatabase moves from one consistent state to another
IsolationConcurrent transactions do not interfere with each other
DurabilityOnce 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:

  1. Lost Update Problem
  2. 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

TimeTransaction T1Transaction T2Value of A in DB
t1READ(A) --> A = 10001000
t2READ(A) --> A = 10001000
t3A = A + 200 (A = 1200)1000
t4WRITE(A)1200
t5A = A - 300 (A = 700)1200
t6WRITE(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

TimeTransaction T1Transaction T2Value of Seats in DB
t1READ(Seats) --> 1010
t2Seats = 10 - 1 = 910
t3WRITE(Seats)9 (uncommitted)
t4READ(Seats) --> 99
t5ROLLBACK10 (restored)
t6Seats = 9 - 1 = 8
t7WRITE(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

ProblemCauseEffect
Lost UpdateTwo transactions read and write the same item concurrentlyOne transaction's update is overwritten and lost
Dirty ReadA transaction reads uncommitted data of another transactionIncorrect 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.