Transaction timeout

Why might a transaction work fine on local, but then get a timeout when deployed. Same DB. Same Node version. Using NextJS deployed in a vercel serverless env. Exact same user. Exact same auth system. Exact same DB. Exact same project. Only real difference is one is running locally and one is deployed to vercel. Even worse is the transaction succeeds. The data is saved and I still get that error.
3 Replies
Nurul
Nurul2mo ago
Could you share the exact error message you are observing? Are the regions in which your code runs locally and the one where you are deploying the same?
Ortharion
Ortharion2mo ago
Transaction API error: Transaction already closed: A commit cannot be executed on an expired transaction. The timeout for this transaction was 5000 ms, however 5069 ms passed since the start of the transaction. Consider increasing the interactive transaction timeout or doing less work in the transaction
Transaction API error: Transaction already closed: A commit cannot be executed on an expired transaction. The timeout for this transaction was 5000 ms, however 5069 ms passed since the start of the transaction. Consider increasing the interactive transaction timeout or doing less work in the transaction
Sorry should have added that. Think I was just frustrated after being stumped all day. I've checked and this only fires once.
const createdUser = await prismaClient.$transaction(async (prisma: PrismaClient) => {

const userService = new UserService(prisma);
const companyService = new CompanyService(prisma);
const { user: callingUser } = await userService.getUserUsingToken(sessionData.token!);

const user = {
email: body.email,
addedBy: callingUser?.id,
companyId: companyId,
firstName: body.firstName,
lastName: body.lastName,
isAdmin: body.isAdmin,
isBillable: body.isBillable ?? true,
isActive: body.isActive ?? true,
departmentId: body.departmentId,
} as Partial<Users>;

return await companyService.addUser(user, companyId);

})

return NextResponse.json({ success: true, user: createdUser });
const createdUser = await prismaClient.$transaction(async (prisma: PrismaClient) => {

const userService = new UserService(prisma);
const companyService = new CompanyService(prisma);
const { user: callingUser } = await userService.getUserUsingToken(sessionData.token!);

const user = {
email: body.email,
addedBy: callingUser?.id,
companyId: companyId,
firstName: body.firstName,
lastName: body.lastName,
isAdmin: body.isAdmin,
isBillable: body.isBillable ?? true,
isActive: body.isActive ?? true,
departmentId: body.departmentId,
} as Partial<Users>;

return await companyService.addUser(user, companyId);

})

return NextResponse.json({ success: true, user: createdUser });
ok i think I might have figured it out. I wrote that endpoint a while back and forgot I had some transactions nested in some of the functions in addUser. So I think transactions within transactions is illegal. However, why does this work on local? Yep, that fixed...but why lol
Nurul
Nurul2mo ago
I am glad to hear that you were able to resolve this.