Transactions
A transaction is a group of database reads and writes that commit together or roll back together. Either the whole transfer happens, or neither row changes.
Detailed Explanation
BEGIN, do work, COMMIT or ROLLBACK. If the process dies before commit, the engine undoes the work. That is atomicity. Isolation levels (READ COMMITTED, REPEATABLE READ, SERIALIZABLE) decide what concurrent transactions see. Durability means a committed write survives a crash (within the database's fsync story).
In Node, a transaction is usually a checked-out client from the pool, not the pool itself. pool.query can send statements on different connections; they will not share a transaction. The pattern is const client = await pool.connect(); try { await client.query("BEGIN"); ...; await client.query("COMMIT"); } catch { await client.query("ROLLBACK"); throw; } finally { client.release(); }. Prisma's prisma.$transaction(async (tx) => { ... }) does the same with a transactional client.
Interactive transactions that await HTTP inside the callback hold a connection and a lock. Keep them short: compute first, then write. Timeouts exist (statement_timeout, Prisma's timeout) so a stuck transaction does not fill the pool.
Idempotency and retries: a commit that timed out from the client's view might have succeeded. Retrying a POST inside a transaction can double-apply unless you have a unique idempotency key.
What it costs you: lock contention and pool exhaustion. A long transaction that touches a hot row blocks others. SERIALIZABLE can throw serialization failures you must retry.
Reach for a transaction when two writes (or a read-then-write) must stay consistent. Reach for a single statement when one UPDATE ... WHERE is enough. Reach for a saga / outbox when the other system is not the same database.
Savepoints (SAVEPOINT, ROLLBACK TO) let you undo one step inside a larger transaction. They are useful when a batch should skip a bad row and keep going. They still hold the outer locks. If the batch is huge, commit in chunks with an idempotency key per chunk instead of one twenty-minute transaction.
Read-only transactions (BEGIN READ ONLY) can use replicas in some setups. Do not put writes in them.
Code Examples
async function transfer(pool, from, to, cents) {
const client = await pool.connect();
try {
await client.query("BEGIN");
await client.query("UPDATE accounts SET cents = cents - $1 WHERE id = $2", [
cents,
from,
]);
await client.query("UPDATE accounts SET cents = cents + $1 WHERE id = $2", [
cents,
to,
]);
await client.query("COMMIT");
} catch (err) {
await client.query("ROLLBACK");
throw err;
} finally {
client.release();
}
}await prisma.$transaction(async (tx) => {
await tx.account.update({
where: { id: from },
data: { cents: { decrement: cents } },
});
await tx.account.update({
where: { id: to },
data: { cents: { increment: cents } },
});
});Common Gotchas
`pool.query` is not a transaction
two queries may use two connections. Hold one `client`.
Do not `await fetch` inside an open transaction
you hold a pool slot and row locks.
A commit timeout is ambiguous
design writes to be idempotent.
Default isolation is usually READ COMMITTED
lost updates are possible without `WHERE` versions or `SELECT FOR UPDATE`.