Reading Something That Was Never True, and Three Relatives
Last timeTwo of Them at Once
There are four named ways concurrent transactions interfere. Each has a schedule you can write in six lines and a precise condition that lets it happen.
The vanishing update from the last lesson is one of several named failures.
This lesson writes out the rest, because the levels in the next lesson are
defined entirely in terms of which of these they forbid.
Reading what was never true
The first is the strangest and, fortunately, the rarest. One transaction
changes a row and has not committed. Another reads it. The first then rolls
back.
| step | time | transaction A | transaction B | committed value | what happened |
|---|---|---|---|---|---|
| 1 | 1 | set the price to 5 | 50 | A has written but not committed. The committed value is still 50; the 5 exists only inside A. | |
| 2 | 2 | read the price, gets 5 | 50 | B sees A uncommitted write. This is the step that a level forbidding dirty reads prevents. | |
| 3 | 3 | charge the customer 5 | 50 | B has now acted in the outside world on the strength of that number. Refunding a card is not something the database can roll back for you. | |
| 4 | 4 | roll back | 50 | A abandons its change, perhaps because a validation failed. The price 5 is gone and was never committed by anybody. | |
| 5 | 5 | commit | 50 | B commits successfully. The customer was charged a price that no committed state of the database ever contained, and no error was raised anywhere. |
The same row, two answers
The second is milder and far more common. A transaction reads a row, carries on
doing other work, and reads the same row again. Between the two reads, somebody
else committed a change to it.
Both values were real. The first was true when it was read and the second was
true when it was read. The transaction nevertheless now holds two different
answers to the same question, and any arithmetic that used the first is
inconsistent with anything using the second.
This matters most in a report. A statement that reads a balance at the top of
the page and the same balance at the bottom, and prints two different numbers,
is not wrong in any individual step and is useless as a statement.
The lesson stops here
4 more paragraphs to go
You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.
The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.
See the planThe contentsThis is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents