Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 37 ยท Transactions

Transaction Basics

Understand transactions before using an ORM: ACID, BEGIN, COMMIT and ROLLBACK, a money-transfer example, savepoints and when you need one.

A transaction groups several statements into one all-or-nothing unit. Either every statement takes effect, or none does. Transferring money is the classic case: subtract from one account, add to another. If the server crashes between those two statements, you must not end up with money that vanished.

ORMs like TypeORM and Sequelize give you helpers for transactions, but the helpers are thin layers over four SQL commands. Learn the commands first and the ORM code in the next lessons will feel obvious. The runnable examples use node:sqlite, so this is real SQL running in a real engine. TypeORM and Sequelize snippets are read-only.

ACID in plain words

  • Atomicity: all statements apply, or none.
  • Consistency: constraints (not null, unique, foreign keys, checks) hold before and after.
  • Isolation: concurrent transactions do not see each other's half-finished work (how strictly depends on the isolation level, see Locking and Isolation).
  • Durability: once committed, the data survives a crash.

BEGIN, COMMIT, ROLLBACK

BEGIN opens a transaction. COMMIT makes the changes permanent. ROLLBACK throws them away. Outside an explicit transaction, each single statement is its own tiny transaction (autocommit).

Node.js A transfer that must not lose money

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

In the second transfer, the first update breaks the check constraint and throws. Nothing was added to Grace yet, but even if the failure had come on the second statement, rollback would have undone the first. That try, commit, catch, rollback shape is the skeleton of every transaction helper you will meet.

Proving that rollback undoes earlier statements

Let the failure happen late, after the first change already ran.

Node.js A late failure undoes an early change

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The ghost order fails at the insert, and the stock update that already ran for ghost matched zero rows anyway, but the pen order is intact: stock 9, one order. If you change the failing case to update a real SKU first, you will see the stock restored by the rollback.

Savepoints

A savepoint is a bookmark inside a transaction. You can roll back to it without abandoning the whole transaction, which is how "nested transactions" are implemented.

Node.js Savepoints

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

TypeORM and Sequelize use savepoints when you nest transaction helpers (in TypeORM, transaction inside a transaction with the same EntityManager reuses the connection; Sequelize supports savepoint through nested transaction options on some dialects).

When do you need one?

Use a transaction when:

  • Several writes must succeed together (order plus order lines plus stock).
  • You read a value and then write based on it (check balance, then debit).
  • A multi-step operation must not be visible half-done to other users.

You do not need one for a single INSERT or UPDATE; the database already makes it atomic.

What it looks like in the ORMs

TypeScript
// TypeORM
await dataSource.transaction(async (manager) => {
  await manager.decrement(Account, { owner: 'Ada' }, 'balance', 30);
  await manager.increment(Account, { owner: 'Grace' }, 'balance', 30);
});
JavaScript
// Sequelize (managed transaction)
await sequelize.transaction(async (t) => {
  await Account.decrement('balance', { by: 30, where: { owner: 'Ada' }, transaction: t });
  await Account.increment('balance', { by: 30, where: { owner: 'Grace' }, transaction: t });
});

Both commit when the callback returns and roll back when it throws. The next lesson goes through the details: Transactions in TypeORM and Sequelize.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does ROLLBACK do?

    Rollback abandons the whole transaction, restoring the state from before BEGIN.
  2. 2. Which letter of ACID says all statements apply or none?

    Atomicity is the all-or-nothing property.
  3. 3. A single INSERT statement: do you need to wrap it in a transaction?

    Autocommit makes each statement atomic on its own.
  4. 4. What does a savepoint let you do?

    ROLLBACK TO a savepoint undoes work after the bookmark while the outer transaction continues.

Summary

  • A transaction makes several statements all-or-nothing.
  • BEGIN, COMMIT, ROLLBACK are the whole mechanism; ORM helpers wrap them in try/catch.
  • Constraint errors inside a transaction should end in a rollback.
  • Savepoints give partial rollback and power nested transactions.
  • Use a transaction for multi-step writes and read-then-write logic.

Next: Transactions in TypeORM and Sequelize