DBMSCSL303 / MAL505
Module 4: Programming, Security & Transactions

Lecture 12: Transactions & ACID Properties

The ACID Guarantees, Transaction Syntax, Concurrency Anomalies, and the 4 ANSI SQL Isolation Levels

Lecture Source Slide Deck

Original Slides: Lecture 12 (51 slides) · Amit Kumar Dhar (IIT Bhilai)
Download lecture12.pdf

1. What is a Transaction?

A Transaction is an atomic sequence of database operations (reads and writes) that represents a single logical unit of work.

       BEGIN TRANSACTION
             |
       [ Operation 1 ]  (Debit Account A)
             |
       [ Operation 2 ]  (Credit Account B)
             |
       Check Constraints
          /     \
       Success   Failure
        /         \
   COMMIT       ROLLBACK

2. The ACID Guarantees

PropertyMeaningSubsystem Responsible
Atomicity"All or Nothing": Either every statement in the transaction executes successfully, or the entire transaction is aborted and rolled back with zero disk trace.Recovery Management & Write-Ahead Log (WAL)
ConsistencyThe transaction must transition the database from one valid integrity state to another valid state, preserving all primary keys, foreign keys, and CHECK rules.Application Logic & Schema Constraints
IsolationIntermediate states of a transaction are completely hidden from other concurrently executing transactions until commit.Concurrency Control (Locking / 2PL / MVCC)
DurabilityOnce a transaction successfully commits, its changes survive permanently on non-volatile storage, even if the database server crashes or loses power a millisecond later.Write-Ahead Log (WAL) & Disk Flush

3. SQL Transaction Control Syntax

BEGIN TRANSACTION;

-- Transfer $500 from Account 101 to Account 202
UPDATE accounts SET balance = balance - 500 WHERE account_id = 101;
UPDATE accounts SET balance = balance + 500 WHERE account_id = 202;

-- If balance drops below zero, abort
-- Otherwise:
COMMIT;

Partial Rollbacks with SAVEPOINT

BEGIN;
INSERT INTO orders VALUES (101, 'Alice');

SAVEPOINT item_insert;
INSERT INTO order_items VALUES (101, 'Laptop', 1200);

-- If stock check fails:
ROLLBACK TO SAVEPOINT item_insert;

COMMIT;

4. The Three Concurrency Anomalies

When transactions execute concurrently without adequate isolation, three distinct data corruption anomalies can emerge:

1. Dirty Read (G1)

  • Scenario: Transaction T1 updates a row without committing. Transaction T2 reads that updated value. T1 subsequently aborts and issues a ROLLBACK.
  • Consequence: T2 made decisions based on phantom data that mathematically never existed!

2. Non-Repeatable Read (G2a / Fuzzy Read)

  • Scenario: Transaction T1 reads a row. Transaction T2 modifies or deletes that row and commits. Transaction T1 reads the exact same row again.
  • Consequence: T1 observes contradictory values for the same row within a single transaction!

3. Phantom Read (G2b)

  • Scenario: Transaction T1 queries a range of rows (e.g., WHERE salary > 80000) and finds 3 matching employees. Transaction T2 inserts a new employee earning 90,000 and commits. T1 re-executes the exact same range query.
  • Consequence: A new "phantom" row appears that was not present in the first read!

5. The Four ANSI SQL Isolation Levels

Isolation LevelDirty ReadNon-Repeatable ReadPhantom ReadTypical Engine Mechanism
READ UNCOMMITTEDAllowedAllowedAllowedRead without locks; fastest, dangerous.
READ COMMITTED (Default in Postgres)PreventedAllowedAllowedReads see only committed snapshots at query start.
REPEATABLE READPreventedPreventedAllowed (Prevented in PG MVCC)Reads see snapshot taken at transaction start.
SERIALIZABLEPreventedPreventedPreventedStrict SSI (Serializable Snapshot Isolation) or 2PL.
-- Setting isolation level in SQL
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;

6. Topic Practice Questions

On this page