PrismaP
Prisma8mo ago
8 replies
Arnór

Transaction API error: Transaction already closed

I'm getting this error in production, but not locally. This is in code that's been running in prod now for about 1-2 years.

Full error message:
Transaction API error: Transaction already closed: A commit cannot be executed on an expired transaction. The timeout for this transaction was 5000 ms, however 5408 ms passed since the start of the transaction. Consider increasing the interactive transaction timeout or doing less work in the transaction.

The thing is that I'm getting this error in like 2 seconds from initiating the transaction. I also have a message about the transaction starting 23 seconds ago.. which makes no sense.

This started happening about 1 week ago.

Environment info:
- fastify app on node
- prisma 6.9.0
- postgresql 15.

The code in question looks like this (i've replaced the table names and fields with fakes, but the code is the same otherwise)

const createResult = await prisma.$transaction(async (tx) => {
  const personEntry = await tx.person.create({
    data: {
      ...personData
    },
  })

  for (const hobby of personHobbies) {
    await tx.person_hobby.create({
      data: {
        hobby_play_times: {
          createMany: {
            data: (hobby.playTime ?? []).map((playTime) => ({
              ...playTime
            })),
          },
        },
      },
    })
  }

  return personEntry
})


I haven't had any issues with other transactions - I'm beginning to suspect that this has something to do with the fact that I'm calling createMany inside of a transaction - and perhaps createMany closes the transaction after its first call. Though I cannot find any notes/docs regarding multiple createMany inside of a single transaction

It's only showing up now, probably because I have a lot more data now, and each insert is taking a little bit longer than it used to.
Was this page helpful?