advanced

Transactions

Choose nested writes, batch transactions, or interactive transactions based on correctness, latency, and retry behavior.

Prisma supports nested writes, array `$transaction([...])`, and interactive `$transaction(async (tx) => ...)`. Pick the smallest API that protects the invariant.

					await prisma.$transaction(async (tx) => {
  await tx.account.update({ where: { id }, data: { balance: { decrement: amount } } });
  await tx.account.update({ where: { id: target }, data: { balance: { increment: amount } } });
});
				

On interviews: compare transaction APIs, retries, isolation, nested service calls, and avoiding external side effects inside interactive transactions.

Common pitfalls: interactive transactions holding connections while app code runs; retries without idempotency duplicating effects.

The trade-off is expressive nested writes versus connection hold time.

Checklist:

  • Pick API by invariant shape.
  • Keep transaction body short.
  • Design retry-safe operations.
  • Commit before external IO when possible.