Transaction Management & ACID
A transaction is a logical unit of work comprising one or more database operations, treated as a single indivisible sequence of actions. Transaction management ensures the correct execution of these operations, particularly in environments with concurrent access and potential system failures.
This guide covers ACID properties, transaction states, isolation levels and concurrency anomalies, write-ahead logging, ARIES recovery, SQL walkthroughs with SAVEPOINT examples, and a 10-question practice quiz.
1Introduction
In database management systems, a transaction is defined as a logical unit of work that comprises one or more database operations. These operations are treated as a single, indivisible sequence -- either all completing successfully (committing) or none at all (aborting/rolling back).
Why do transactions matter? They enforce data integrity by preventing the database from entering an inconsistent state. They enable concurrency, allowing multiple users to access and modify data simultaneously. And they provide a mechanism for recovery after system crashes, ensuring committed changes are not lost and aborted changes are fully undone.
Consider an online banking system. When a user transfers money from account A to account B, this involves deducting funds from A and adding them to B. These two operations must happen together; if one succeeds and the other fails, the bank's books would be inconsistent. A transaction ensures this “all-or-nothing” behavior, guaranteeing that either the full transfer completes, or it's entirely cancelled, maintaining financial integrity.
2Key Definitions
Essential terms for understanding transaction management at the university level.
Transaction
A logical unit of work: a sequence of read/write operations treated as a single, indivisible step
ACID Properties
Atomicity, Consistency, Isolation, Durability -- the four guarantees for reliable transaction processing
Commit
Signals successful completion; all changes are made permanent and visible
Rollback (Abort)
Signals unsuccessful completion; all changes are undone, restoring previous state
Transaction Log
Sequential, append-only file on stable storage recording all modifications and events
Checkpoint
A synchronization point where dirty pages and log records are flushed to stable storage
Recovery Manager
DBMS component that restores the database to a consistent state after a failure
Concurrency Control
Mechanism ensuring correct execution of concurrent transactions while preserving consistency
Serializable Schedule
A schedule equivalent to some serial schedule -- the gold standard for correctness
Isolation Level
Setting that defines the degree of isolation from other concurrent transactions
Deadlock
Two or more transactions indefinitely waiting for each other to release a resource
Savepoint
A named mark within a transaction to which a partial ROLLBACK can be performed
Write-Ahead Logging (WAL)
Protocol: log record must be written to stable storage before data page is written to disk
Dirty Page
A data page modified in the buffer pool but not yet written back to stable storage
3ACID Properties Deep Dive
The ACID properties are the cornerstone of reliable transaction processing, formally introduced by Andreas Reuter and Theo Härder in 1983. They ensure the correctness and robustness of database systems.
Atomicity (All-or-Nothing)
A transaction is treated as a single, indivisible unit. Either all operations complete successfully, or none are applied.
Example: In a fund transfer, if the debit succeeds but the credit fails, atomicity ensures the debit is reversed.
Consistency (Valid States)
A transaction transforms the database from one valid state to another, preserving all integrity constraints, domain constraints, and business rules.
Example: A withdrawal that would make a balance negative is rejected to preserve the non-negative balance constraint.
Isolation (No Interference)
Concurrent transactions execute as if each were the only one running. Intermediate effects are not visible to other transactions until commit.
Example: Two concurrent inventory updates produce the same correct result as if they ran one after the other.
Durability (Permanent Changes)
Once committed, changes persist even after system failures. Achieved via the transaction log and write-ahead logging (WAL).
Example: After a payment commits, the record persists even if the server immediately crashes.
Modern distributed systems often embrace BASE (Basically Available, Soft state, Eventually consistent) as an alternative to strict ACID. BASE prioritizes high availability and partition tolerance over immediate strong consistency, and is common in NoSQL databases. The choice between ACID and BASE depends on your application's consistency requirements.
4Transaction States & State Diagram
A transaction progresses through five primary states from initiation to completion.
Transaction State Machine
Every transaction follows this lifecycle from BEGIN to TERMINATED.
1. Active
The initial state. The transaction is executing its operations (READ, WRITE, UPDATE, INSERT, DELETE). Entered via BEGIN TRANSACTION.
2. Partially Committed
After the last operation executes. Changes are still in volatile memory (buffer cache), not yet on stable storage.
3. Committed
All changes are permanently recorded on stable storage. Changes are durable and visible to other transactions.
4. Aborted
The transaction failed. All changes are undone (rolled back). Can occur from Active or Partially Committed states due to errors, crashes, or explicit ROLLBACK.
5. Terminated
The final state reached after either Committed or Aborted. The transaction is no longer active in the system.
Savepoints
Savepoints provide partial rollback within a single transaction. A SAVEPOINT sets a named marker; ROLLBACK TO SAVEPOINT undoes only the changes made since that savepoint.
SAVEPOINT savepoint_name; ROLLBACK TO SAVEPOINT savepoint_name; RELEASE SAVEPOINT savepoint_name; -- Removes the savepoint
5Isolation Levels & Anomalies
Databases offer different isolation levels that let developers choose a trade-off between strict isolation and performance. Each level prevents a different set of concurrency anomalies.
Concurrency Anomalies
Dirty Read
Reading data written by another uncommitted transaction. If it aborts, you read “dirty” data that never existed.
Non-Repeatable Read
Reading the same row twice and getting different values because another committed transaction modified it between reads.
Phantom Read
Re-executing a query and getting a different set of rows because another transaction inserted or deleted matching rows.
Isolation Levels Table
| Isolation Level | Dirty Read | Non-Repeatable | Phantom |
|---|---|---|---|
| Read Uncommitted | Possible | Possible | Possible |
| Read Committed | Prevented | Possible | Possible |
| Repeatable Read | Prevented | Prevented | Possible |
| Serializable | Prevented | Prevented | Prevented |
Serial vs Serializable Schedules
T1: A = A + 50 | T2: A = A × 2 | Initial: A =