7 Transaction Management

Database Management System ยท Unit 7

Transaction Management

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

Transaction Management in short

A transaction groups several database actions into one logical unit of work, such as a bank transfer. This chapter explains the rules that keep a database correct when transactions run together or fail halfway.

Transaction Management: notes

Transaction definition

A transaction is a sequence of operations that performs one logical task and moves the database from one consistent state to another. It starts with BEGIN and ends with COMMIT (save all changes) or ROLLBACK (undo all changes).

The basic operations are:

  • read(X): copy data item X from the database to a program variable.
  • write(X): write the value back to the database.

Example: transfer 500 from account A to account B.

BEGIN TRANSACTION;
UPDATE Account SET Balance = Balance - 500 WHERE AccNo = 'A';
UPDATE Account SET Balance = Balance + 500 WHERE AccNo = 'B';
COMMIT;

If the system fails after the first update, money would vanish. A transaction prevents this by making both updates succeed together or not at all.

ACID properties

The ACID properties guarantee reliable transactions.

  • Atomicity: all operations happen or none happen. The recovery manager undoes partial work.
  • Consistency: a transaction moves the database from one valid state to another valid state. Total money in A and B stays the same after the transfer.
  • Isolation: transactions running together do not affect each other. Each behaves as if it ran alone. The concurrency control manager ensures this.
  • Durability: once committed, changes stay even after a crash. The recovery manager uses logs to ensure this.

Transaction states and state diagram

A transaction passes through these states.

  • Active: the first state. The transaction is executing.
  • Partially committed: the last statement has run, but changes are not yet safely stored.
  • Committed: the changes are permanently saved.
  • Failed: normal execution cannot continue because of an error.
  • Aborted: the transaction is rolled back and the database is restored to its state before the transaction.
  • Terminated: the transaction has left the system.
              +--> Partially committed --> Committed --+
              |                                        |
Begin --> Active                                       +--> Terminated
              |                                        |
              +--> Failed --> Aborted -----------------+

From Active, a transaction goes to Partially committed or to Failed. A partially committed transaction can still fail if the final write to disk fails. An aborted transaction may be restarted or killed.

What this unit covers

  • Transaction definition
  • ACID properties
  • Transaction states and state diagram
  • Serial and non-serial schedules
  • Serializable schedules
  • Conflict serializability testing
  • Lost update problem
  • Dirty read problem
  • Incorrect summary problem

Lost update problem

2082 ยท 10 marks

What is a transaction? Describe how lost update and dirty read problems occur in concurrent execution of transactions? Illustrate with examples.[10]

Show solved answer

Transaction, Lost Update, and Dirty Read Problems

What is a Transaction?

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).

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.

Open this answer on its own page โ†’

0 ยท 5 marks

Show how lost update and incorrect summary problem might occur in concurrent execution of transactions. [5]

The lost update problem occurs when two transactions read the same data item and then both update it, causing one transaction's update to be overwritten (lost) by the other. Let X = 100 (a bank account balance). Two transactions T1 and T2 execute concurrently: Time T1 T2 X (in DB) ---------------...

Open this answer on its own page โ†’

Serial and non-serial schedules

2080 ยท 10 marks

Define serial, non-serial and serializable schedules with example. How can you test serializability in a schedule? Explain with an example.[10]

Show solved answer

Serial, Non-Serial, and Serializable Schedules

1. Serial Schedule

A serial schedule is one in which transactions are executed one after another, without any interleaving of operations. One transaction must complete entirely before the next one begins.

Properties:

  • No concurrency
  • Always consistent (correct by definition)
  • Poor performance due to no overlap

Example:

StepT1T2
1Read(A)
2Write(A)
3Read(B)
4Write(B)
5Read(A)
6Write(A)
7Read(B)
8Write(B)

Here T1 executes completely before T2 starts. This is a serial schedule: T1 โ†’ T2.


2. Non-Serial Schedule

A non-serial schedule is one in which operations of multiple transactions are interleaved with each other. Transactions execute concurrently.

Properties:

  • Allows concurrency and better throughput
  • May or may not produce consistent results
  • Not all non-serial schedules are correct

Example:

StepT1T2
1Read(A)
2Read(A)
3Write(A)
4Write(A)
5Read(B)
6Write(B)
7Read(B)
8Write(B)

Here operations of T1 and T2 are interleaved. This is a non-serial schedule.


3. Serializable Schedule

A serializable schedule is a non-serial schedule that produces the same result as some serial schedule of the same transactions. It is the standard criterion for correctness of concurrent execution.

A non-serial schedule S is serializable if it is equivalent to some serial schedule.

Types of Serializability:

  • Conflict Serializability (most commonly tested)
  • View Serializability

4. Testing Serializability: Precedence Graph (Conflict Graph) Method

Concept of Conflicting Operations

Two operations conflict if:

  1. They belong to different transactions
  2. They access the same data item
  3. At least one of them is a Write
Operation PairConflict?
Read(X) - Read(X)No
Read(X) - Write(X)Yes
Write(X) - Read(X)Yes
Write(X) - Write(X)Yes

Steps to Test Conflict Serializability

Step 1: Identify all pairs of conflicting operations between different transactions.

Step 2: Build a Precedence Graph (Serialization Graph):

  • One node for each transaction
  • Draw a directed edge from Ti โ†’ Tj if an operation of Ti conflicts with an operation of Tj, and Ti's operation appears before Tj's operation in the schedule

Step 3: Check for cycles in the graph:

  • No cycle โ†’ Schedule is conflict serializable
  • Cycle exists โ†’ Schedule is NOT conflict serializable

Worked Example

Consider the following schedule S with transactions T1, T2, T3:

StepT1T2T3
1Read(X)
2Read(X)
3Write(X)
4Read(X)
5Read(Y)
6Write(Y)
7Write(Y)
8Write(X)

Step 1: Identify Conflicting Pairs

ConflictOperationsEdge
T2: Write(X) before T1: Read(X)Both on X, Write involvedT2 โ†’ T1
T1: Read(X) at step 1 before T2: Write(X) at step 3Both on X, Write involvedT1 โ†’ T2
T2: Write(Y) before T3: Write(Y)Both on Y, Write involvedT2 โ†’ T3
T3: Read(Y) before T2: Write(Y)Both on Y, Write involvedT3 โ†’ T2

Step 2: Draw Precedence Graph

T1 ---------> T2
 ^             |
 |             v
 +---- T2    T3 <----> T2

More precisely:

  • T1 โ†’ T2 (T1 reads X before T2 writes X)
  • T2 โ†’ T1 (T2 writes X before T1 reads X at step 4)
  • T2 โ†’ T3 (T2 writes Y before T3 writes Y)
  • T3 โ†’ T2 (T3 reads Y before T2 writes Y)

Step 3: Check for Cycles

  • T1 โ†’ T2 โ†’ T1 forms a cycle
  • T2 โ†’ T3 โ†’ T2 forms a cycle

Conclusion: The schedule contains cycles in the precedence graph, therefore it is NOT conflict serializable.


Example of a Serializable Schedule

StepT1T2
1Read(A)
2Write(A)
3Read(A)
4Write(A)
5Read(B)
6Write(B)

**Conflicting

Open this answer on its own page โ†’

2078 ยท 5 marks

What is Schedule? Explain Serializability and Conflict Schedule. [5]

--- A schedule is a sequence of operations (read, write, commit, abort) from one or more concurrent transactions, arranged in the order they are executed by the system. - If transactions T1, T2, ..., Tn execute concurrently, a schedule defines the interleaved order of their operations. - A schedu...

Open this answer on its own page โ†’

0 ยท 5 marks

Define serial, non-serial and serilizable schedules with example. How can you test serializability in a schedule, illustrate with an example?[5]

A schedule in which transactions are executed one after another, without any interleaving of operations. One transaction completes fully before the next begins. Example (Serial Schedule S1): T1 T2 -------- Read(A) Write(A) Read(B) Write(B) Read(A) Write(A) Here T1 executes completely before T2 st...

Open this answer on its own page โ†’

Transaction states and state diagram

2080 ยท 5 marks

What is Transaction? State and explain the states of transaction with transition diagram.[5]

A transaction is a logical unit of work or a sequence of operations performed on a database that must be executed as a whole. It is a collection of database operations (such as read, write, insert, update, delete) that together form a single logical unit. A transaction must satisfy the ACID prope...

Open this answer on its own page โ†’

2079 ยท 10 marks

What is Transaction? Draw a Transaction state diagram and discuss the typical state that a transaction goes through during execution.[10]

A transaction is a logical unit of work that consists of a sequence of one or more database operations (such as read, write, insert, update, or delete) that must be executed as a single, indivisible unit. A transaction either executes completely or not at all, ensuring the database moves from one...

Open this answer on its own page โ†’

Transaction definition

2078 ยท 5 marks

What is Transaction? State and explain the properties of Transaction.[5]

A transaction is a logical unit of work that consists of a sequence of one or more database operations (such as read, write, insert, update, or delete) that must be executed as a single atomic unit. A transaction either completes fully or does not execute at all, ensuring the database moves from ...

Open this answer on its own page โ†’