Database transactions, ACID and isolation: testing for half-saved data
A save that is half done leaves bad data. Learn what a transaction promises and how to test for half-saved data when steps fail or two users act at once.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: moving a book between two shelves. You lift a book off shelf A. You set it on shelf B. If the lights fail while it is in your hand, it sits on neither shelf. Both steps should finish, or neither step should.
Daily List is a to-do list app. Lily moves Buy milk from her Home list to her Work list. The app must take the task off one list and put it on the other. Tom tests the gap between those two steps. (SYNTHETIC / TRAINING ONLY: Daily List and every person here are invented.).
What it is: A is a group of (Structured Query Language) changes that all stay, or all get undone. SQL is the language for asking a database questions and for changing it. A table is a grid. A row is one record, such as one task. A column is one fact, such as the list name. BEGIN opens the group. COMMIT keeps it. ROLLBACK throws it away. With autocommit enabled and no surrounding driver transaction, one completed statement commits on its own, so a later failure cannot undo it. A driver may instead open a transaction implicitly; check the connection settings.
Why it matters: Without one transaction, a failure after the delete leaves Buy milk on no list. Lily loses the task, and the screen may still say the move worked. An decides how much one open transaction can see of another. Two saves can both pass a if each one reads the old count.
The ACID (atomicity, consistency, isolation and durability) properties name the four promises. They are atomic, consistent, isolated and durable. Tom turns those four words into four tests.
PLAIN WORDS · 1 in this section
- autocommit
- mode where each statement saves at once
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the software.
THE REAL JOB Testing that a save either happens fully or not at all, even when something fails or two people act together.
- Stop it half way
- Query the rows
- Run two at once
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| Taking a book off shelf A | The first step of the move |
| Putting it on shelf B | The second step of the move |
| Lights going out in between | A failure between the steps |
WHERE THIS PICTURE STOPS
- A person sees the book in their hand, but a half-saved change may be invisible.
- Two people at one shelf can see each other, but two requests cannot.
- A book is one thing, but a database change may touch many rows and other systems.
HOOK
All or nothing, then check the rows.
REMEMBER IT THIS WAY
A sequence: the real steps, in order.
Break it, then look: stop it half way, query the rows, then run two at once. Does the data show all or nothing?
| In the trick | What it stands for |
|---|---|
| Stop it half way | A failure between the two steps of the move |
| Query the rows | Check the data, since a change that stopped half way may be invisible |
| Run two at once | Two people act together |
TELL A FRIEND
Moving a book between shelves in one go, so it never ends up on neither or both.
OFTEN MIXED UP WITH Concurrency and race-condition testing ideas. The race conditions lesson is about timing bugs in general. This lesson uses the database promises to decide what should be true after such a clash.
PRACTISE AND KEEP
Sources for this topic — 6 sources
- Market practice
Transactions (PostgreSQL tutorial) ↗ — PostgreSQL Global Development Group · Tutorial section on transactions: BEGIN and COMMIT group steps so that they happen as one; ROLLBACK cancels the updates so far; a failure part-way leaves none of the steps applied; each statement outside BEGIN is its own transaction (search excerpt, page not opened)
PostgreSQL18 publisher documentation opened on6November2026: §3.4 explicit transactions and savepoints. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
Transaction Isolation (PostgreSQL documentation, current version) ↗ — PostgreSQL Global Development Group · Transaction isolation page: four standard levels are accepted but PostgreSQL implements three distinct ones; Read Uncommitted behaves as Read Committed so dirty reads cannot happen; Read Committed is the default and gives each command a fresh snapshot; Repeatable Read does not allow phantom reads in PostgreSQL; Serializable also watches for results that no one-at-a-time order could give (search excerpt, page not opened)
PostgreSQL18 publisher documentation opened on6November2026: §13.2 isolation levels and Read Committed snapshots. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
A Critique of ANSI SQL Isolation Levels (MSR-TR-95-51) ↗ — Microsoft Research · Research paper critiquing the ANSI SQL isolation level definitions; used only to say that the level names alone do not settle which problems are possible (search excerpt, page not opened)
Berenson, Bernstein, Gray, Melton, O'Neil and O'Neil, June 1995. Jim Melton was the SQL standard's own editor, which is why a paper finding fault with ANSI SQL-92 is a usable route to what ANSI SQL-92 said.
Cited as an industry paper, not a standard: it argues against the definitions it reproduces, so a citation has to be clear about which half it is relying on — the quotation of the standard, or the critique of it.
- Implementation-specific
PostgreSQL documentation ↗ — The PostgreSQL Global Development Group · PostgreSQL 18 official manual, opened 2026-11-06: tutorial-transactions §3.4 and transaction-iso §13.2. Supports only PostgreSQL behavior; earlier execution evidence remains separately versioned.
An implementation, not the standard — which is the point. ISO/IEC 9075-2 is paywalled, and this documents the same constructs in text anybody can open. Its isolation-level chapter attributes to the standard explicitly, so it can carry a standard-level claim as long as the citation says which document it is quoting.
Cited as market practice rather than an official standard by kind: an engine's manual is authoritative about that engine and is not the obligation. Where a question turns on behaviour PostgreSQL chose rather than behaviour SQL requires, the answer has to say so.
One correction carried here because it circulates widely: the sentence "The fundamental difference between WHERE and HAVING…", quoted all over the internet as PostgreSQL documentation, is not in the current documentation. It was searched for in the source tree and is not there.
Read through a mirror, because the publisher's own domain could not be fetched (a reachability gap, not a statement about the publisher). A 403 there is a policy refusal and says nothing about whether the document exists or is reachable elsewhere.
Anyone on an unrestricted network should open the canonical URL above and confirm the section and page numbers cited against it. Every claim drawn from this entry is written so that it can be checked against a numbered page.
Read from the
postgres/postgresdocumentation source tree (doc/src/sgml), which the project itself maintains and which carries the section ids the published URLs are derived from — so a cited section id can be checked against the rendered page. - Implementation-specific
SQLite official documentation — constraints, types and transactions ↗ — SQLite · SQLite rolling official documentation, opened 2026-11-06: foreignkeys.html, datatype3.html, lang_createtable.html, lang_transaction.html, isolation.html, cli.html, series.html and lang_createtrigger.html as relevant. Historical SQLite 3.40.1 results remain distinct from in-memory audit witnesses on 3.53.1.
Relevant primary pages opened 6 November2026; rolling documentation and CLI examples (3.54.0) are distinct from historical lesson runs3.40.1 and audit witnesses3.53.1. No file/connected database audit execution.
- Implementation-specific
Python sqlite3 — Transaction control ↗ — Python Software Foundation · Python sqlite3 official transaction-control documentation, opened 2026-11-06: legacy implicit DML transactions and isolation_level=None for explicit SQL control. Witness runtime Python3.12.14; rolling docs display3.15.2.
Primary page opened2026-11-06; rolling docs display3.15.2, audit runtime3.12.14. Explicit SQL control uses isolation_level=None in the lesson.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.