UNIHIVE

Study Hub UNIPORT CSC 480

CSC 480 · Database Management

Transactions, concurrency control and recovery

Transactions and the ACID properties, problems of concurrent access, serialisability, locking and two-phase locking, deadlock, timestamp methods, and recovery with logs and checkpoints.

Our notes

3 min read

Transactions

A transaction is a logical unit of work: a sequence of reads and writes that must be done completely or not at all.
Example. Transfer ₦20,000 from a student's wallet to a shop: debit wallet; credit shop. If the system crashes between the two, money vanishes unless both are treated as one transaction.
START TRANSACTION; UPDATE wallet SET balance = balance - 20000 WHERE id = 7; UPDATE wallet SET balance = balance + 20000 WHERE id = 12; COMMIT;
(ROLLBACK undoes everything since START TRANSACTION.)

ACID

  • Atomicity: all or nothing (recovery manager).
  • Consistency: a transaction takes the database from one valid state to another (integrity rules).
  • Isolation: concurrent transactions do not see each other's partial work (concurrency control).
  • Durability: once committed, changes survive crashes (logs, disk).

Problems without concurrency control

  • Lost update: T1 and T2 both read balance ₦50,000; T1 adds 10,000 and writes 60,000; T2 subtracts 5,000 and writes 45,000. T1's update is lost (correct result 55,000).
  • Dirty read (uncommitted dependency): T2 reads a value T1 wrote, then T1 rolls back.
  • Inconsistent analysis (non-repeatable read): a total is computed while another transaction moves money between accounts.
  • Phantoms: new rows appear between two reads of the same query.

Serialisability

A schedule (interleaving of operations) is serialisable if its effect equals some serial (one-after-another) order. Conflict serialisability is tested with a precedence graph: draw an edge Ti → Tj when an operation of Ti conflicts with (same item, at least one write) and comes before one of Tj. No cycle = serialisable.

Locking

  • Shared (S) lock to read; exclusive (X) lock to write. Many S locks can coexist; X excludes all others.
  • Two-phase locking (2PL): a transaction acquires all its locks before releasing any (growing then shrinking phase). 2PL guarantees conflict serialisability. Strict 2PL holds X locks until commit, preventing dirty reads and cascading rollbacks.
  • Granularity: database, table, page or row locks (finer = more concurrency, more overhead).

Deadlock

T1 holds a lock on A and waits for B; T2 holds B and waits for A. Handling:

  • Detection: a wait-for graph; a cycle means deadlock; abort one victim.
  • Prevention: timestamps (wait-die, wound-wait), ordering resource requests, timeouts.

Other methods

Timestamp ordering (older transactions win conflicts), optimistic concurrency (check for conflicts only at commit: good when conflicts are rare), multiversion concurrency control (MVCC: readers see a snapshot and never block writers: used by PostgreSQL and InnoDB).
SQL isolation levels: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ (MySQL's default), SERIALIZABLE.

Recovery

Failures: transaction errors, system crashes (memory lost), media failure (disk lost).

  • Log (journal): records before- and after-images of every change, written to stable storage before the data page (write-ahead logging, WAL).
  • After a crash: redo committed transactions whose changes may not have reached disk; undo uncommitted ones.
  • Checkpoints limit how far back recovery must read.
  • Media failure: restore the last backup and replay the log. Keep backups off the server (a weekly download, as many small sites do).

Exams usually ask: define transaction and ACID; illustrate lost update, dirty read and inconsistent analysis with schedules; test serialisability with a precedence graph; explain 2PL and strict 2PL; deadlock detection and prevention; log-based recovery with checkpoints.

Common mistake: confusing consistency (ACID) with isolation. Consistency is about rules being kept; isolation is about concurrent transactions not interfering.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All CSC 480 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.