Prisma Client: Sequential vs Interactive Transactions
Decide when to use Prisma's sequential $transaction([...]) versus interactive $transaction(async (tx) => {}) for atomic writes.
04 Jun 2026, 10:15 UTC

The problem: a write that depends on a read
Imagine an order service that must check product stock before creating an order. If you read stock with one Prisma Client call and create the order with another, another request can buy the last item between the two calls. You need both operations inside one database transaction so either both succeed or neither does.
Prisma Client offers two transaction APIs: sequential $transaction([...]) and interactive $transaction(async (tx) => {}). The right choice depends on whether later operations need results from earlier ones.
Use sequential transactions for a fixed list of writes
Sequential transactions accept an array of Prisma operations. Prisma sends them as one atomic unit. If any promise rejects, the whole transaction rolls back automatically; you do not call commit or rollback yourself.
This fits cases where operations are independent or can be prepared up front. For example, creating several related records when every value is already known:
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function createAuditEntries(userId: string, actionIds: string[]) {
return prisma.$transaction([
prisma.auditLog.create({
data: { userId, action: 'BATCH_EXPORT', createdAt: new Date() },
}),
...actionIds.map((actionId) =>
prisma.auditItem.create({
data: { actionId, status: 'QUEUED' },
})
),
]);
}
Because every operation is defined before execution, the database either applies all of them or applies none. This form is less natural when an insert's generated ID is needed by a later insert, unless you use nested writes.
Use interactive transactions for read-then-write logic
Interactive transactions pass a scoped transaction client, usually named tx, into an async function. You can await a read, inspect the result, and issue a later write using the same tx client. All operations share the same database transaction.
async function reserveStock(productId: string, quantity: number) {
return prisma.$transaction(async (tx) => {
const product = await tx.product.findUnique({
where: { id: productId },
});
if (!product || product.stock < quantity) {
throw new Error('Insufficient stock');
}
return tx.product.update({
where: { id: productId },
data: { stock: { decrement: quantity } },
});
});
}
The tx argument is critical. Calling prisma.product.update on the outer client inside that function may run outside the transaction, breaking atomicity.
Trade-offs and connection-pool pressure
Interactive transactions hold a database connection from the start of the callback until the final promise resolves. If you await file I/O, HTTP requests, or slow business logic inside the callback, you block a connection pool slot and increase lock contention. Keep the callback limited to database queries.
Prisma does not support nested transactions. Calling $transaction inside an active interactive transaction is not a supported pattern. Behavior also differs by provider: PostgreSQL, MySQL, SQL Server, and SQLite support relational transactions, while MongoDB multi-document transactions require a replica set and have different isolation characteristics.
How to verify your choice
- Run
npx prisma generatein your project directory and inspect the generatedPrismaClienttype for$transactionsignatures matching your installed Prisma version. - Write a test that throws inside
$transaction([...])and assert that none of the intended rows were inserted or updated. - Enable query logging for your database provider and confirm a single
BEGIN/COMMITor equivalent wraps the grouped operations. - Review your provider's documentation for transaction support and provider-specific limitations before deploying.
Commands assume a Node.js project with Prisma installed and database access configured. Running migrations or schema changes requires appropriate database credentials; test on a non-production database first.
Decision checklist
- Can you prepare every operation before execution? Use sequential
$transaction([...]). - Does a later write depend on an earlier read? Use interactive
$transaction(async (tx) => {}). - Are you avoiding non-database awaits inside the callback? Required for interactive transactions.
- Are you on MongoDB without a replica set? Multi-document transactions are not available.
Pick sequential transactions for simple, fixed batches. Pick interactive transactions when you must read before you write, but keep the callback short and database-only to protect your connection pool.