---
title: Transactions
description: A mutation and its nested mutations commit or roll back together.
---

A transaction groups the writes of a mutation into one commit.

<Diagram caption="A top-level mutation and its nested mutations share one transaction and one outcome.">
  <Fan>
    <Node slot="from" title="Transaction" subtitle="Starts with one mutation" tone="accent">
      <Check>Nested mutations join</Check>
      <Check>Nested queries read its state</Check>
    </Node>
    <Node title="Commit" subtitle="Events go to listeners" />
    <Node title="Roll back" subtitle="Discards writes and events" tone="muted" />
  </Fan>
</Diagram>

```ts
async handler(ctx, input) {
  const booking = await findBooking(ctx, input.bookingId);
  await ctx.run(availability.hold, rangeOf(booking));
  return setStatus(ctx, booking.id, "confirmed");
}
```

- **Entry.** The registry checks the top-level policy and validates input before it opens the transaction.
- **Nesting.** Use `ctx.run` to share a mutation's transaction, including across modules.
- **Commit (1).** [Delivery](/docs/concepts/events) starts after commit.
- **Roll back (2).** An uncaught failure rolls back the mutation.
- **Independent commits.** Run each mutation from an action, listener, or cron when one item must commit or fail on its own.

## Caught failures

If a caller catches a nested mutation's error and returns, commit throws with the failure path and the original error as `cause`. You can catch a nested query's application error and still commit.

Keep external effects in actions, listeners, and crons. A savepoint can roll back its writes and leave events queued in the outer transaction.

## After the mutation returns

A mutation's `ctx` works only while its transaction is open. If the handler starts work and does not await it (a timer, a promise), that work cannot use `ctx.db`, `ctx.emit`, `ctx.schedule` or `ctx.run` after the mutation returns. Each one throws, and names the mutation.

Await the work in the handler, or make it separate work: emit an event, or use `ctx.schedule`.

Drivers do not agree here. node-postgres runs a late statement on the connection it gave back, outside a transaction or inside the next request's. The library stops the statement first, on every adapter.

## Lost races are retried

A retryable failure restarts the entire mutation in a fresh transaction and rebuilds events. Even a caught nested mutation failure can cause a retry.

Postgres adapters retry deadlocks (`40P01`) and serialization failures (`40001`). The default is three attempts with a short random pause. Set `database.transactionAttempts` to `1` for one attempt. A job's `attempts` controls job retries separately.

## Concurrent writes

Lock rows in a consistent order, such as by ID, to avoid deadlocks.

If a rule depends on rows that do not exist yet, lock before you check. The bookings example takes a Postgres advisory lock per property. An exclusion constraint also stops overlapping stays.

## See also

[Events](/docs/concepts/events), [Errors](/docs/concepts/errors), [Adapters](/docs/reference/adapters)
