Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

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 show

Fix it fast

Most common cause, 1 of 2

The callback runs longer than the interactive transaction timeout

Do slow work before the transaction starts and keep only database writes inside it.

Fix
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 },
);
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Prisma ORM 7.10, Prisma ORM 6.19, Prisma ORM 5.22 (older message)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
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:

  1. 1The callback runs longer than the interactive transaction timeoutThe 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
  2. 2A tx query is not awaited, so it runs after Prisma has committedThe 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.

Check
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.

Fix
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.

Verify
node --test tests/billing.test.js

Confirmed by prisma.io

Did this fix it?

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.

Check
grep -rnE "forEach\(async|tx\.[a-zA-Z]+\.[a-zA-Z]+\(" src/ | grep -v await

Fix

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.

Fix
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.

Verify
node server.js

Confirmed by prisma.io

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.