Database transactions ensure reliable, concurrent operations on data. A transaction is a sequence of operations treated as a single unit: all succeed (commit) or all fail (rollback).
ACID Properties
ACID guarantees transaction reliability:
Atomicity: All operations succeed or none do. Example: Transfer $100 from A to B: debit A, credit B. If debit fails, rollback both.
Consistency: Transaction brings DB from one valid state to another (enforces constraints). Example: Balance can't go negative; transaction checks and enforces.
Isolation: Concurrent transactions don't interfere (detailed below). Example: Two transfers don't see half-complete changes.
Durability: Committed changes persist, even after crashes (via WAL/logs). Example: Power failure post-commit → data safe on restart.
Isolation Levels and Anomalies
Isolation levels balance concurrency vs. consistency (ANSI SQL standard). Higher levels prevent more anomalies but reduce throughput.
Level
Description
Anomalies Prevented
Read Uncommitted
Reads any data, even uncommitted.
None
Read Committed
Reads only committed data (no dirty reads).
Dirty reads
Repeatable Read
Same tx sees same data (no non-repeatable reads).
Dirty + non-repeatable reads
Serializable
Fully isolated, as if txs run sequentially.
All (incl. phantoms)
Anomalies (problems from low isolation):
Dirty Read: Read uncommitted data (rolled back later). Ex: Tx1 debits A (100→0), Tx2 reads 0,Tx1rollsback→Tx2seesphantom0.
Non-Repeatable Read: Read same row twice, different values. Ex: Tx1 reads balance 100;Tx2updatesto90 & commits; Tx1 re-reads $90.