Transaction Atomicity: Where All or Nothing Surprises You
Atomicity means all or nothing, but autocommit, errors that do not abort, and DDL that commits implicitly can surprise you. Examples in MySQL and PostgreSQL.
In this chapter
All or nothing, in practice: where transactions surprise you
More on Transactions: ACID
Keep learning
Overview
chapters · transactions, isolation and locks All or nothing, in practice: where transactions surprise you ACID promises all-or-nothing, but real code has five gaps. Forgot BEGIN Each statement already saved alone. COMMIT after error Half the transfer saved. Crash mid-job Restart takes ages. ALTER TABLE midway Earlier work saved without asking. Stock below zero Every statement valid, rule still broken.
1. No BEGIN, no transaction
Default is autocommit: each statement saves the moment it finishes. Autocommit on (default) Autocommit off First statement quietly opens a transaction.Forget COMMIT → open for hours, holding locks. Connection drops → everything undone Never saves “whatever was done so far”.
2. One statement fails: what gets undone?
An error inside an open transaction MySQL style: stays open PostgreSQL style: broken Duplicate key / rule broken Lock wait timeout usually only that statement undone Deadlock victim whole tx undone → retry from BEGIN You COMMIT anyway acts as ROLLBACK: nothing saved credit id 2 the mistake debit saved $100 lost nothing saved Replay it: pick the engine and the error, see what COMMIT saves. Any error → ROLLBACK Same behaviour on every engine. Deadlock / timeout → retry Start again from BEGIN. SAVEPOINT sp Database only Sent email or queued message stays sent.
3. The server crashes mid-transaction
💥 crash before credit restart: read log no commit record money back Same transfer as ACID. Without undo: $100 missing. inventory before seen inside session 1 only after kill (PostgreSQL) Cost of undoing a big transaction (crash, ROLLBACK or killed session: same bill) undo work grows with changes can take minutes–hours replays log on restart MySQL/InnoDB, SQL Server, Oracle (undo log) millions of rows one huge tx chunks of a few thousand each commits resumable job restart where stopped The habit for batch jobs.
4. Some statements save your work without asking
CREATE, ALTER, DROP, TRUNCATE behave differently per engine. Toggle and try a rollback. Saves your tx first, then runs. ROLLBACK can’t undo any of it. MySQL migrations Not all-or-nothing: make each step re-runnable. ORMs creating tables mid-request Can save half your work. INSERT / UPDATE / DELETE / SELECT Never save on their own inside a tx.
5. Rules the database checks, and rules it can't see
Checked per statement (usual) NOT NULL, UNIQUE, CHECK, foreign keys. Deferrable: checked at COMMIT Lets two rows point at each other. Constraints see one row at a time, not two buyers at once. buyer A buyer B SELECT stock → 1 sold twice 0 (2 sold) Conditional update A lock Covered later in this module. Strictest isolation Lenient modes Cut bad values, only warn. Keep strict on. FK checks off Turning back on won’t re-check old rows.
Quick reference
Transaction statements What it does Watch out starts a transaction inside an open one: MySQL commits the old one first after error: MySQL saves what worked, PostgreSQL nothing undoes the whole tx can’t undo auto-saved schema changes or emails SAVEPOINT / ROLLBACK TO gone after COMMIT or full ROLLBACK Autocommit off next statement starts a tx easy to leave open for hours
Remember
No BEGIN Every statement saves itself. Error → ROLLBACK Deadlock/timeout → retry. Never COMMIT after error. Crash undoes unfinished Schema changes apart In MySQL they save your work first. Cross-row rules Need concurrency tools, not constraints. → next: what goes wrong when transactions overlap
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.