Transaction boundaries
Local baseline: Node.js 22.16.0, ECMAScript modules, synthetic data. Run each complete example independently as node main.mjs in a disposable directory. Static ByteMind blocks do not imply live Node execution. SQLite is experimental in this baseline. Third-party integrations below are staged candidates, not installed or validated dependencies.
Roll back an inventory change on failure
import {DatabaseSync} from 'node:sqlite';const db=new DatabaseSync(':memory:');try{ db.exec("CREATE TABLE stock(n INTEGER CHECK(n>=0)); INSERT INTO stock VALUES(5); CREATE TABLE orders(id TEXT PRIMARY KEY NOT NULL); INSERT INTO orders VALUES('o1')"); db.exec('BEGIN');try{ db.exec('UPDATE stock SET n=n-2'); db.prepare('INSERT INTO orders VALUES(?)').run('o1'); db.exec('COMMIT'); }catch(e){db.exec('ROLLBACK');console.log('rolled back');} console.log(db.prepare('SELECT n FROM stock').get().n); console.log(db.prepare('SELECT count(*) AS n FROM orders').get().n);}finally{db.close();}Expected output:
rolled back51The duplicate order violates the primary key and rollback restores stock.
How it works
Check a business condition in the write itself, for example a stock threshold or expected version. Inspect the affected row count; zero may mean the item is missing, stock is insufficient or a concurrent update changed its version. In a real API distinguish conflicts from absence without leaking unauthorized data. Isolation defines how transactions observe competing work and differs across engines and isolation levels. SQLite serializes writers; the second fixture simulates stale version observations sequentially, not simultaneous connections. Retry the whole unit only when safe, with a bounded policy. Keep transactions short and avoid waiting for external services inside them.
Reject a stale version with a conditional update
import {DatabaseSync} from 'node:sqlite';const db=new DatabaseSync(':memory:');try{ db.exec('CREATE TABLE stock(id INTEGER PRIMARY KEY,n INTEGER,version INTEGER); INSERT INTO stock VALUES(1,5,0)'); const update=db.prepare('UPDATE stock SET n=n-?,version=version+1 WHERE id=1 AND version=? AND n>=?'); console.log(update.run(2,0,2).changes); console.log(update.run(1,0,1).changes); console.log(JSON.stringify(db.prepare('SELECT n,version FROM stock').get()));}finally{db.close();}Expected output:
10{"n":3,"version":1}The first change succeeds; the second uses a stale version and affects zero rows.
Common mistakes
Do not treat a read followed by a separate unchecked write as safe concurrency control. Do not catch a constraint error and commit partial work by accident. Local atomicity does not extend to an unrelated service.
Practice and next steps
Modify the inventory fixture to request more items than remain. Assert that both inventory and orders are unchanged, then test a valid order and a stale version update.
Further reading: https://nodejs.org/download/release/v22.16.0/docs/api/sqlite.html. Related staged reference: node-transaction-boundaries-lookup (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Transaction boundaries — knowledge check
Apply the lesson to a fresh scenario and identify the failure boundary.
An order insert fails after stock is reduced in the same transaction. What restores stock?
What does zero affected rows mean for the version-checked write?
Does the local SQLite transaction roll back a remote HTTP request?