Prisma P2028: a query runs on an interactive transaction that already ended
Your error
Transaction API error: Transaction already closed: Could not perform operation.2 more lines it can showFix it fast
Most common cause, 1 of 2The callback runs longer than the interactive transaction timeout
Do slow work before the transaction starts and keep only database writes inside it.
const pdf = await renderInvoice(order); // slow work first, outside
await prisma.$transaction(
async (tx) => {
await tx.invoice.create({ data: { orderId: order.id, file: pdf } });
await tx.order.update({ where: { id: order.id }, data: { status: "BILLED" } });
},
{ maxWait: 5000, timeout: 15000 },
);Short answer
Transaction already closed (Prisma P2028) means a query reached an interactive $transaction that had ended. Either the callback outlived the timeout, 5000 ms by default, and was rolled back, or an un-awaited query arrived after Prisma committed. Move slow work out or raise timeout, and await every tx call. Applies to Prisma ORM 5, 6 and 7.
Which cause do you have?
Read the text after Transaction API error. An expired transaction with two millisecond numbers points to the timeout; committed or rolled back points to a query that ran after the callback finished. Prisma 5 can answer with the generic Could not perform operation, which hides the case, so time the callback as well.
console.time("tx");
await prisma.$transaction(async (tx) => {
// ... the same work as before
});
console.timeEnd("tx");Then find the line you see in this list and open its fix:
- 1The callback runs longer than the interactive transaction timeout
The reason says expired transaction and the second number is larger than the first, or the timer in the first step prints more than 5000 ms - 2A tx query is not awaited, so it runs after Prisma has committed
The reason says committed transaction or rolled back, and the failing line sits inside forEach, a .then chain, a timer, or code that kept tx after the callback returned
What does the error look like?
Transaction API error: Transaction already closed: Could not perform operation.Transaction already closed: A query cannot be executed on an expired transaction. The timeout for this transaction was 5000 ms, however 6182 ms passed since the start of the transaction. Consider increasing the interactive transaction timeout or doing less work in the transaction.Transaction already closed: A query cannot be executed on a committed transaction.
Cause 1 of 2
The callback runs longer than the interactive transaction timeout
You seeThe reason says expired transaction and the second number is larger than the first, or the timer in the first step prints more than 5000 ms
Check
Prisma gives every interactive transaction a time limit, 5000 ms unless you pass another value. When the callback is still running at that point, Prisma rolls the transaction back, and the next tx query fails with P2028: an expired-transaction message on Prisma 6 and 7, Could not perform operation on Prisma 5. Prisma's own docs warn against network requests and slow queries inside the callback for exactly this reason.
grep -rn "\$transaction(async" src/Fix
Do slow work before the transaction starts and keep only database writes inside it. If the writes alone need longer, pass a timeout and a maxWait as the second argument.
const pdf = await renderInvoice(order); // slow work first, outside
await prisma.$transaction(
async (tx) => {
await tx.invoice.create({ data: { orderId: order.id, file: pdf } });
await tx.order.update({ where: { id: order.id }, data: { status: "BILLED" } });
},
{ maxWait: 5000, timeout: 15000 },
);Verify
The timer from the first step stays under the timeout you set, and the error no longer appears under the same load.
node --test tests/billing.test.jsConfirmed by prisma.io
Cause 2 of 2
A tx query is not awaited, so it runs after Prisma has committed
You seeThe reason says committed transaction or rolled back, and the failing line sits inside forEach, a .then chain, a timer, or code that kept tx after the callback returned
Check
Prisma commits an interactive transaction as soon as the async callback finishes. A tx call that nobody awaits, for example inside array.forEach(async ...), keeps running after that moment and lands on a committed transaction. The same happens when tx is saved to a variable or handed to a background job and used later.
grep -rnE "forEach\(async|tx\.[a-zA-Z]+\.[a-zA-Z]+\(" src/ | grep -v awaitFix
Await every tx query before the callback returns. Use for...of, or await Promise.all over the mapped promises, and never let tx leave the callback.
await prisma.$transaction(async (tx) => {
// wrong: items.forEach(async (i) => tx.stock.update(...))
for (const i of items) {
await tx.stock.update({
where: { sku: i.sku },
data: { qty: { decrement: i.qty } },
});
}
});Verify
Run the code path again. All writes appear in the database together and no P2028 is logged.
node server.jsConfirmed by prisma.io
How do I stop it coming back?
- Set timeout and maxWait once in a shared helper that wraps $transaction, so every call uses values chosen on purpose.
- Turn on the typescript-eslint rule no-floating-promises so a tx call written without await is flagged in review.
- Keep network calls, uploads and email sending out of transaction callbacks; run them after the commit succeeds.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.