본문 바로가기
Q&A2026년 4월 19일2분 읽기

Prisma transaction 안에서 $queryRaw 썼더니 "already in a transaction" 에러가 납니다

주니어개발자소연
조회 200댓글 3

Prisma로 트랜잭션 안에서 raw SQL을 섞어 쓰려는데, 이런 에러가 발생합니다.

PrismaClientKnownRequestError:
Transaction API error: Transaction already closed:
A query cannot be executed on a transaction which has been committed or rolled back.

코드는 이렇게 생겼습니다.

await prisma.$transaction(async (tx) => {
  await tx.user.update({ where: { id }, data: { balance: { decrement: 100 } } });
  await prisma.$queryRaw`UPDATE ledger SET ...`;   // ← 이거 문제?
  await tx.transfer.create({ data: { ... } });
});

동시에 DB lock이 길어진다는 경고도 같이 뜹니다. 트랜잭션 안에서 raw query를 섞는 올바른 방법이 있나요?

댓글 0

아직 댓글이 없습니다.
Ctrl+Enter로 등록