Skip to content

Transactions and isolation: recap and quiz

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. Deadlocks. Transactions that wait for each other in a cycle cannot continue. The database aborts one, and the application retries it.
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”
  1. 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)
  2. Which rules does the database not know? For each, say which transaction checks it and at what isolation level. (The C of ACID)
  3. 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)
  4. 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)
  5. 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.
  6. Does the application retry a serialization failure and a deadlock? It has to rerun the whole transaction from its first read. (Deadlocks)
  7. Do all transactions take their rows in the same order? One piece of code that does not can bring the deadlock back.
  8. 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)

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.

Question 1 of 8

You ask PostgreSQL for the read uncommitted level. What do you get?

Choose one answer.

Question 2 of 8

A balance is 100. T1 and T2 both read it. T1 writes 100 + 30 and commits. Then T2 writes 100 + 20 and commits. What is the balance?

Enter a number and choose its unit.

balance
Question 3 of 8

Two doctors each count who is on call, see two, and take themselves off. Both commit at repeatable read with row versions, and nobody is on call. Why did the database let it happen?

Choose one answer.

Question 4 of 8

Which of these statements about deadlocks are true? Choose every answer that applies.

Choose every answer that applies.

Question 5 of 8

In a deadlock, T2's transfer of 20 from B to A was aborted. T1 committed, so A is 90 and B is 110. T2 is retried. What is A afterwards?

Enter a number and choose its unit.

in A
Question 6 of 8

A long report reads many rows at repeatable read while a writer wants to update one of them. What happens?

Choose one answer.

Question 7 of 8

An application runs on PostgreSQL with its default level. It is moved to MySQL's InnoDB and nobody sets a level. What changes?

Choose one answer.

Question 8 of 8

A rule says that at least one doctor must be on call. The database has no constraint for it. Who has to make sure it holds?

Choose one answer.

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.