Prisma có hai kiểu transaction:
- Sequential — truyền một mảng thao tác:
prisma.$transaction([op1, op2]). Chạy tuần tự, một cái fail thì rollback hết. Đơn giản nhưng không có logic điều kiện. - Interactive — truyền một hàm async:
prisma.$transaction(async (tx) => {
const user = await tx.user.create({ ... })
if (user.role === 'admin') await tx.adminProfile.create({ ... })
})Dùng được if/else. Lưu ý: bên trong phải gọi qua tx (không phải prisma) thì các thao tác mới nằm chung một transaction.
Vài điểm thêm:
- Nested write tự động bọc trong transaction: prisma.user.create({ data: { profile: { create: {...} } } }).
- Đặt được isolation level và timeout (mặc định 5s).
Giới hạn: Prisma không làm transaction trải nhiều database — với microservices phải dùng 2-phase commit hoặc saga pattern.
Prisma has two transaction styles:
- Sequential — pass an array of operations:
prisma.$transaction([op1, op2]). Runs sequentially, rolls everything back if one fails. Simple but has no conditional logic. - Interactive — pass an async function:
prisma.$transaction(async (tx) => {
const user = await tx.user.create({ ... })
if (user.role === 'admin') await tx.adminProfile.create({ ... })
})Lets you use if/else. Note: inside it you must call via tx (not prisma) for the operations to belong to the same transaction.
A few extras:
- Nested writes are auto-wrapped in a transaction: prisma.user.create({ data: { profile: { create: {...} } } }).
- You can set the isolation level and a timeout (default 5s).
Limitation: Prisma does not do transactions spanning multiple databases — for microservices use a 2-phase commit or the saga pattern.