Transactions and isolation: recap and quiz
The module in five sentences
Section titled “The module in five sentences”- ACID and the transaction. A transaction bundles statements so that all of them take effect or none does, and the C is kept by the database for the rules it knows and by the application for the rest.
- Five anomalies. Two transactions that run at once can read a change that is not committed, read a row or a search twice and get two answers, overwrite each other, or break a rule together without touching the same row.
- Isolation levels. A level is a list of anomalies the database promises to stop. The names come from the standard, and what they mean depends on the product, so its documentation decides.
- Locks and row versions. Locks make transactions take turns. Row versions let readers see an older copy. Two writers of the same row still conflict.
- Deadlocks. Transactions that wait for each other in a cycle cannot continue. The database aborts one, and the application retries it.
How each one breaks
Section titled “How each one breaks”| Idea | What breaks | What you see | What helps |
|---|---|---|---|
| Atomicity | A transaction stops half way, or there is none | Money debited and never credited | Put every related change in one transaction |
| Isolation | Two transactions overlap at a level too weak for them | A wrong result and no error | The level the requirement needs, or an explicit lock |
| Locks | A reader holds a lock a writer needs | Writers wait for a long transaction | Short transactions |
| Row versions | Two writers, or a rule over several rows | A serialization failure, or write skew | Retry, or serializable |
| Deadlock | Opposite lock orders | One transaction aborted | One order everywhere, and a retry |
A checklist for the transactions of an application
Section titled “A checklist for the transactions of an application”- Is every group of related changes in one transaction? If a crash between two statements would leave the data wrong, they belong together. (ACID and the transaction)
- Which rules does the database not know? For each, say which transaction checks it and at what isolation level. (The C of ACID)
- Which anomaly could each read-then-write sequence meet? Read a value, compute, write it back is the shape of a lost update. Check on a row that two requests can reach at once. (Five anomalies)
- What level does the application actually run at? The default differs between products, and the same name does not mean the same guarantee. (Isolation levels)
- Did you test your own schedule on your own database? The documentation says what is promised. A test shows what happens in the case you care about.
- Does the application retry a serialization failure and a deadlock? It has to rerun the whole transaction from its first read. (Deadlocks)
- Do all transactions take their rows in the same order? One piece of code that does not can bring the deadlock back.
- How long does the longest transaction stay open? With locks it makes writers wait. With row versions it keeps old versions alive. (Locks and row versions)
Check your understanding
Section titled “Check your understanding”Eight questions cover the whole module. Every option is explained, whether it is right or wrong.
Module quiz: transactions and isolation
This quiz needs JavaScript to check your answers.
Where this goes next
Section titled “Where this goes next”Everything in this module happened on one database. The next module copies the data to other machines, and then the questions come back in a harder form: what does a reader on a replica see while a transaction is still being copied, and what does a committed write mean if the machine that took it disappears.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.