Transaction Processing
Intro, System Concepts, States, Logging
A transaction is a logical unit of work — one or more DB operations that must execute atomically and leave the DB in a consistent state.
| State | What's Happening |
|---|---|
| Active | Operations executing normally |
| Partially Committed | Last op done; data still in memory buffer (not disk yet) |
| Committed | All changes written permanently to disk ✅ |
| Failed | Error / constraint violation / deadlock — can't continue |
| Aborted | Rolled back; DB restored to pre-transaction state |
| Terminated | Transaction lifecycle complete (committed or aborted) |
System Log (Transaction Log) ▼ expand
| Log Entry | Meaning |
|---|---|
| [start_transaction, T] | T has begun |
| [write_item, T, X, old, new] | T wrote X; old→new value |
| [read_item, T, X] | T read data item X |
| [commit, T] | T committed successfully |
| [abort, T] | T was aborted / rolled back |
WAL = write LOG first, then DB. "Partially Committed" ≠ "Committed" — data still in buffer, not disk.
ACID Properties
All or nothing · Constraints hold · Isolated from others · Disk survives crash
Schedules, Serializability & Recoverability
| Pair | Conflicts? |
|---|---|
| Read – Read | ❌ NO — safe |
| Read – Write | ✅ YES |
| Write – Read | ✅ YES |
| Write – Write | ✅ YES |
- Create a node for each transaction
- Draw edge T1 → T2 if any op of T1 conflicts with a later op of T2
- If graph has NO cycle → Conflict Serializable ✅
- If graph has a cycle → NOT serializable ❌
| Type | Rule | Avoids |
|---|---|---|
| Serial | No interleaving | Everything — always correct |
| Strict | No R/W until writer commits/aborts | Dirty R/W, cascading rollback, easiest recovery |
| Cascadeless | Read only after writer commits | Dirty reads, cascading rollback |
| Recoverable | T2 commits after T1 (if T2 read T1's data) | Committed dirty reads |
| Serializable | Equivalent to some serial schedule | Incorrect concurrent results |
Every Serial schedule is Strict. Every Strict is Cascadeless. Every Cascadeless is Recoverable. But NOT vice versa!
Dirty Read = reading uncommitted data. Cascading Rollback = chain rollback of multiple Ts that read each other's dirty data.
Transaction Support in SQL
| Command | What It Does |
|---|---|
| BEGIN / START TRANSACTION | Marks start of transaction |
| COMMIT | Save all changes permanently; release locks |
| ROLLBACK | Undo ALL changes since BEGIN |
| SAVEPOINT name | Set named checkpoint for partial rollback |
| ROLLBACK TO SAVEPOINT name | Undo only to savepoint (keep rest) |
| RELEASE SAVEPOINT name | Remove a savepoint |
START TRANSACTION; UPDATE ACCOUNT SET Balance = Balance - 5000 WHERE Acc_No = 'A101'; SAVEPOINT after_debit; -- checkpoint here UPDATE ACCOUNT SET Balance = Balance + 5000 WHERE Acc_No = 'B202'; -- if credit fails: ROLLBACK TO SAVEPOINT after_debit; -- undo credit only -- if all ok: COMMIT;
| Isolation Level | Dirty Read | Non-Repeatable Read | Phantom Read |
|---|---|---|---|
| READ UNCOMMITTED | ✅ Possible | ✅ Possible | ✅ Possible |
| READ COMMITTED | ❌ Prevented | ✅ Possible | ✅ Possible |
| REPEATABLE READ | ❌ Prevented | ❌ Prevented | ✅ Possible |
| SERIALIZABLE | ❌ Prevented | ❌ Prevented | ❌ Prevented |
Dirty Read prevented at READ COMMITTED. Non-Repeatable prevented at REPEATABLE READ. Phantom prevented only at SERIALIZABLE.
Concurrency Control — 2PL
| Held ↓ / Requested → | S (Read) | X (Write) |
|---|---|---|
| S (Read) | ✓ Compatible | ✗ Block |
| X (Write) | ✗ Block | ✗ Block |
| Variant | When Released | Key Property |
|---|---|---|
| Basic 2PL | During execution (shrinking phase) | Serializable; cascading rollbacks possible |
| Conservative 2PL | All locks acquired BEFORE start | Deadlock-free; low concurrency |
| Strict 2PL ⭐ | X-locks held until commit/abort | Most widely used; prevents dirty reads |
| Rigorous 2PL | ALL locks held until commit/abort | Strictest; serializes by commit time |
Deadlock & Timestamp Ordering
Wait-Die: T1 waits.
Wound-Wait: T1 aborts T2 immediately.
| Scheme | Older requests younger's lock | Younger requests older's lock |
|---|---|---|
| Wait-Die | WAIT | DIE |
| Wound-Wait | WOUND (abort younger) | WAIT |
| Aspect | 2PL (Locking) | Timestamp Ordering |
|---|---|---|
| Mechanism | Acquire/release locks | TS comparison on each op |
| Deadlock | Possible ❌ | Impossible ✅ |
| Rollbacks | Less frequent | More frequent ❌ |
| Serializability | Guaranteed ✅ | Guaranteed ✅ |
| Overhead | Lock management | Timestamp management |
Flashcards
Tap to flip • All key exam concepts
Master Cheatsheet
| Topic | Key Formula / Rule |
|---|---|
| ACID-A | All or Nothing → undo log |
| ACID-C | Valid state → Valid state → constraints |
| ACID-I | No interference → locking |
| ACID-D | Committed = permanent → WAL + disk |
| Conflict | Diff T + Same item + ≥1 Write = CONFLICT |
| Serializable? | Precedence graph — NO cycle = YES |
| WAL | Log BEFORE data write |
| S lock | Read; many can share |
| X lock | Write; exclusive — no sharing |
| 2PL | Growing phase (acquire) → Lock Point → Shrinking phase (release) |
| Strict 2PL | Hold X-locks until commit/abort (most common) |
| Wait-Die | Older waits; Younger dies |
| Wound-Wait | Older wounds younger; Younger waits |
| Deadlock detect | Wait-for graph → cycle → abort victim |
| Thomas Write Rule | Obsolete write → IGNORE (don't abort) |
| Dirty Read | Prevented at READ COMMITTED+ |
| Non-Repeatable | Prevented at REPEATABLE READ+ |
| Phantom Read | Prevented at SERIALIZABLE only |
Glossary of Shorthands
Fast meaning lookup for symbols and abbreviations used in this module.
| Short Form | Meaning | Example / Context |
|---|---|---|
| T | Transaction | T1, T2 are two concurrent transactions |
| TS(T) | Timestamp of transaction T | Smaller TS means older transaction |
| RTS(X) | Read Timestamp of item X | Latest TS of any transaction that read X |
| WTS(X) | Write Timestamp of item X | Latest TS of any transaction that wrote X |
| R(X) | Read operation on item X | Read item X from DB/buffer |
| W(X) | Write operation on item X | Write updated value of X to DB |
| S-lock | Shared lock (read lock) | Multiple transactions may hold it together |
| X-lock | Exclusive lock (write lock) | Only one transaction can hold it |
| 2PL | Two-Phase Locking | Growing phase then shrinking phase |
| WAL | Write-Ahead Logging | Write log record before writing data item |
| WFG | Wait-For Graph | Cycle in WFG indicates deadlock |
| ACID | Atomicity, Consistency, Isolation, Durability | Core transaction correctness properties |
| DBMS / RDBMS | Database Management System / Relational DBMS | Software that stores and manages structured data |
T = transaction, TS = transaction age/order, RTS/WTS = latest read/write time on a data item.