CSC 480 · Database Management
Our notes
3 min readTransactions
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
-
PostgreSQL tutorial: Transactions
(opens postgresql.org in a new tab)
-
ACID
(opens en.wikipedia.org in a new tab)
Wikipedia, ACID, CC BY-SA 4.0 · licence
-
Two-phase locking
(opens en.wikipedia.org in a new tab)
Wikipedia, Two-phase locking, CC BY-SA 4.0 · licence
Watch
-
Best pick Lec-88: ACID Properties of a Transaction | Database Management System
14 min · ≈ 63 MB · about ₦35
(opens youtube.com in a new tab)
-
Lec-96: Introduction to Serializability | Transactions Concurrency and Control | DBMS
9 min · ≈ 41 MB · about ₦23
(opens youtube.com in a new tab)
-
Lec-102: 2 Phase Locking(2PL) Protocol in Transaction Concurrency Control | DBMS
10 min · ≈ 45 MB · about ₦25
(opens youtube.com in a new tab)
-
Lec-119: Immediate Database Modification in DBMS | Log Based Recovery Methods
6 min · ≈ 27 MB · about ₦15
(opens youtube.com in a new tab)
-
ACID Properties in Databases With Examples
5 min · ≈ 23 MB · about ₦13
(opens youtube.com in a new tab)
-
#17 - Concurrency Control Theory (CMU Intro to Database Systems)
1 h 21 min · ≈ 365 MB · about ₦205
(opens youtube.com in a new tab)
-
#17 - Two-Phase Locking Concurrency Control (CMU Intro to Database Systems)
1 h 5 min · ≈ 293 MB · about ₦165
(opens youtube.com in a new tab)
-
CMU Database Systems - 21 Database Recovery (Fall 2018)
1 h 17 min · ≈ 347 MB · about ₦195
(opens youtube.com in a new tab)
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.