पाठ 23 / 35
Transaction APIs in Prisma, TypeORM and Sequelize
यह पाठ अभी हिंदी में अनुवादित नहीं है, इसलिए अंग्रेज़ी में दिखाया जा रहा है।
Write a wallet transfer as a transaction in each ORM and avoid the classic mistake of running a query outside the transaction object.
लेखक: Rahul Agarwal · 15 साल से वेब डेवलपर
Callbacks that commit or roll back
All three ORMs offer a callback-style transaction: you pass an async function, the ORM opens a transaction, runs it, commits if the function resolves and rolls back if it throws. Prisma $transaction(async (tx) => { ... }) is called an interactive transaction; every query inside must use tx, not the outer prisma, and it has a timeout so long-running work does not hold locks. Prisma also accepts an array of queries, $transaction([q1, q2]), for independent writes that need no logic between them. TypeORM dataSource.transaction(async (manager) => { ... }) gives an EntityManager bound to the transaction. Sequelize sequelize.transaction(async (t) => { ... }) gives a transaction object that you must pass as { transaction: t } to every query, unless you have configured continuation-local storage. The most common bug in all three is the same: one query inside the callback accidentally uses the global client, so it runs outside the transaction and is not rolled back.
Tracing a Prisma interactive transaction
Asha (balance 300) tries to send 500. Follow how the thrown error turns into a rollback.
await prisma.$transaction(async (tx) => {
const from = await tx.wallet.update({
where: { id: ashaId },
data: { balance: { decrement: 500 } },
});
if (from.balance < 0) throw new Error('Insufficient balance');
await tx.wallet.update({ where: { id: raviId }, data: { balance: { increment: 500 } } });
});
- Line 1: Prisma opens a transaction and hands the callback a client, tx, bound to it.
- Line 2: The debit runs inside the transaction. Other connections still see 300 until commit.
- Line 6: The returned row shows a negative balance, so the code throws instead of continuing.
- Line 7: This credit never runs, because the throw left the callback early.
- Line 8: The callback rejected, so Prisma rolls back and rethrows the error to the caller.
The same transfer in TypeORM and Sequelize
Every statement goes through manager or carries transaction: t. Without that, it would commit on its own.
// TypeORM
await dataSource.transaction(async (manager) => {
await manager.decrement(Wallet, { id: ashaId }, 'balance', 500);
await manager.increment(Wallet, { id: raviId }, 'balance', 500);
});
// Sequelize (managed transaction)
await sequelize.transaction(async (t) => {
await Wallet.decrement('balance', { by: 500, where: { id: ashaId }, transaction: t });
await Wallet.increment('balance', { by: 500, where: { id: raviId }, transaction: t });
});Keep transactions short
Do not call payment gateways, send emails or wait for user input inside a transaction. Locks are held until commit, so slow work blocks other requests. Do the external call first or afterwards, and keep the transaction to the database writes that must be atomic.
त्वरित जाँच: Inside a Sequelize managed transaction, a query is written without { transaction: t } and CLS is not configured. What happens to it if the transaction later rolls back?
- It is rolled back too, because it ran inside the callback
- It ran on a separate connection outside the transaction, so its change is not rolled back
- Sequelize refuses to run it and throws
- It is retried automatically after the rollback
Answer
It ran on a separate connection outside the transaction, so its change is not rolled back — Being inside the callback is not enough. Without the transaction option (or CLS), Sequelize runs the query outside the transaction, so it commits independently.