Database Transactions in Prisma
From the Prisma ORM cheat sheet · Transactions & Raw Queries · verified Jul 2026
Database Transactions
Ensure data consistency with interactive and sequential transactions
typescript
// Sequential transaction (auto-rollback on error)
const result = await prisma.$transaction([
prisma.user.create({ data: { email: 'user@example.com' } }),
prisma.post.create({ data: { title: 'Post' } }),
])
// Interactive transaction with custom logic
await prisma.$transaction(async (tx) => {
const user = await tx.user.create({
data: { email: 'user@example.com' }
})
if (user.email.includes('spam')) {
throw new Error('Spam detected')
}
await tx.post.create({
data: { title: 'Post', authorId: user.id }
})
})💡 Transactions auto-rollback on any error
⚠️ Keep transactions short to avoid locks
📌 Use interactive transactions for complex logic
✅ Set appropriate timeout and isolation levels