Skip to content
Chethan Yadav_
Database Management Systems

DBMS/Lesson 6 of 9/5 min read

Transactions and ACID

Transactions, ACID properties and how each is implemented, transaction states, schedules, conflict and view serializability, precedence graphs, and recoverable, cascadeless and strict schedules.


On this page

Transaction

A transaction is a logical unit of work made of one or more operations that must execute as a whole. Classic example: transfer 100 from A to B.

BEGIN;
UPDATE account SET balance = balance - 100 WHERE id = 'A';
UPDATE account SET balance = balance + 100 WHERE id = 'B';
COMMIT;   -- or ROLLBACK on error

At the storage level a transaction is a sequence of read(X) and write(X) operations ending in commit or abort.

ACID

Transactions guarantee four properties.

Atomicity

All or nothing. If the transfer fails after debiting A, the debit is undone. Implemented by the recovery manager using undo logs (see logging and recovery).

Consistency

A transaction takes the database from one valid state to another, preserving all constraints (keys, foreign keys, checks, and business rules like "total money is unchanged"). It is the joint responsibility of the application (correct logic) and the DBMS (constraints).

Isolation

Concurrent transactions do not see each other's intermediate states; the result is as if they ran one after another. Implemented by the concurrency control manager using locks or MVCC (see concurrency control). Strength is tunable with isolation levels.

Durability

Once committed, changes survive crashes and power loss. Implemented by forcing the log to stable storage before acknowledging commit (write-ahead logging), plus backups and replication.

PropertyComponent responsible
AtomicityRecovery manager (undo log)
ConsistencyApplication + integrity constraints
IsolationConcurrency control (locks, MVCC)
DurabilityRecovery manager (redo log, WAL)

Transaction States

active --> partially committed --> committed
   |               |
   v               v
 failed  ----->  aborted   (then restart or kill)
  • Active: executing.
  • Partially committed: last statement executed, changes may still be in memory buffers.
  • Committed: changes are durable.
  • Failed: normal execution cannot continue.
  • Aborted: rolled back; the database is restored to the state before the transaction. It can be restarted (if the error was not logical) or killed.

Schedule

A schedule is the order in which operations of concurrent transactions are executed.

  • Serial schedule: transactions run one after another, no interleaving. Always correct but slow.
  • Non-serial (concurrent) schedule: operations interleave. Faster, but may produce wrong results.

Serializability

A concurrent schedule is serializable if its effect equals some serial schedule. Serializability is the standard correctness criterion for isolation.

Conflict Serializability

Two operations conflict if they belong to different transactions, access the same data item, and at least one is a write:

PairConflict?
read / readNo
read / writeYes
write / readYes
write / writeYes

A schedule is conflict serializable if it can be turned into a serial schedule by swapping adjacent non-conflicting operations.

Precedence Graph

Test for conflict serializability:

  1. One node per transaction.
  2. Edge Ti -> Tj if an operation of Ti conflicts with and comes before an operation of Tj.
  3. Acyclic graph means conflict serializable; a topological order gives the equivalent serial schedule.

Example:

T1: R(A)        W(A)
T2:       R(A)        W(A)

T1 R(A) before T2 W(A) gives T1 -> T2. T2 R(A) before T1 W(A) gives T2 -> T1. Cycle, so not conflict serializable. This is the lost update problem.

View Serializability

A weaker condition. Two schedules are view equivalent if they have the same initial reads, the same reads-from relationships, and the same final writes. Every conflict serializable schedule is view serializable, but not vice versa; the extra ones contain blind writes (writes without a prior read). Testing view serializability is NP-complete, so databases use conflict serializability.

Recoverability

Serializability is not enough; schedules must also behave well when transactions abort.

Recoverable Schedule

If Tj reads data written by Ti, then Ti must commit before Tj commits. Otherwise, if Ti aborts after Tj committed, Tj used data that never existed and cannot be undone.

Cascading Rollback

If T2 reads uncommitted data from T1 and T1 aborts, T2 must also roll back, and so on: one abort cascades through many transactions. Wasteful.

Cascadeless Schedule

Transactions only read data written by committed transactions. Prevents cascading rollbacks. Also called avoiding cascading aborts (ACA).

Strict Schedule

A transaction can neither read nor overwrite a data item until the last transaction that wrote it has committed or aborted. Makes undo simple (just restore the before-image). Strict two-phase locking produces strict schedules.

strict  ⊂  cascadeless  ⊂  recoverable

Quick revision

  • ACID: atomicity (undo), consistency (constraints), isolation (concurrency control), durability (redo, WAL).
  • Conflicts need different transactions, same item, at least one write.
  • Precedence graph acyclic = conflict serializable.
  • Strict ⊂ cascadeless ⊂ recoverable.