As example shows you need to use prisma instance that is passed as an argument to your callback function, like that:
async createMerchant(data: Prisma.MerchantCreateInput): Promise<Merchant> {
return await this.prisma.$transaction(async (prisma): Promise<Merchant> => {
// Not this.prisma, but prisma from argument
await prisma.merchant.create({
data,
});
throw new Error(`Some error`);
});
}
Answer from Danila on Stack OverflowMedium
medium.com › @moiserushanika2006 › mastering-database-rollbacks-with-prismas-transactional-finesse-9156b8319bb1
Mastering Database Rollbacks with Prisma’s Transactional Finesse | by Moise rushanika | Medium
December 23, 2023 - In this example, we use the $transaction method to define an array of Prisma queries to be executed within the same transaction. If any query within the transaction fails, the entire transaction is rolled back. The catch block handles errors ...
Prisma
prisma.io › home › transactions and batch queries › transactions and batch queries › transactions and batch queries › transactions and batch queries
Transactions and batch queries (Reference) | Prisma Documentation
This ensures that your application commits multiple concurrent or parallel transactions as if they were run serially. When a transaction fails due to a write conflict or deadlock, Prisma Client returns a P2034 error. In your application code, add a retry around your transaction to handle any P2034 errors, as shown in this example:
Manual transaction commit, rollback
Problem Since Prisma does not offer manual transaction rollbacks which is one of the reason TypeORM is preferred by users over Prisma. I intended to perform an E2E test on the production database, ... More on github.com
A transaction rollback should go through Prisma middleware
Problem We use Prisma middleware to audit log database operations. If we're running those operations in a transaction, and the transaction rolls back, the middleware has no way to catch the rollback. This results is operations which were... More on github.com
Rollback transaction manually
Add an extra parameter to your this.i18n methods and pass the prisma transaction client to it. More on github.com
Support manual rollback in interactive transactions
When in a transaction block, be able to call rollback on the prisma client. More on github.com
GitHub
github.com › prisma › prisma › issues › 22309
Manual transaction commit, rollback · Issue #22309 · prisma/prisma
December 7, 2023 - type PrismaFlatTransactionClient = Prisma.TransactionClient & { $commit: () => Promise<void>; $rollback: () => Promise<void>; }; @Injectable() export class TransactionInterceptor implements NestInterceptor { readonly ROLLBACK = { [Symbol.for('prisma.client.extension.rollback')]: true, }; public readonly prisma = new PrismaClient({ log: ["query"] }).$extends({ client: { async $begin() { const prisma = Prisma.getExtensionContext(this); let setTxClient: (txClient: Prisma.TransactionClient) => void; let commit: () => void; let rollback: () => void; // a promise for getting the tx inner client cons
Author prisma
GitHub
github.com › prisma › prisma › discussions › 13564
Prisma support for transaction rollback and Promise.all · prisma/prisma · Discussion #13564
Hey team, we stumbled across an issue when trying to run Promise.all() within a $transaction and rolling back once a promise fails. This is what our implementation looks like: prisma.$transaction((...
Author prisma
Prisma Client Python
prisma-client-py.readthedocs.io › en › stable › reference › transactions
Transactions - Prisma Client Python
Prisma Client Python supports interactive transactions, this is a generic solution allowing you to run multiple operations as a single, atomic operation - if any operation fails, or any other error is raised during the transaction, Prisma will roll back the entire transaction.
DEV Community
dev.to › reyronald › dealing-with-open-database-transactions-in-prisma-3clk
Dealing with open database transactions in Prisma - DEV Community
February 23, 2025 - After that, any and all subsequent ... or ROLLBACK statement is found. There's no need to specify any form of transaction identifier in future statements, and there's no additional reference to the open transaction either. In my solution, calling runInDbTransaction represents that BEGIN statement, and closing the scope of its callback argument represents the COMMIT phase to end the transaction. Although I understand Prisma's decision ...
GitHub
github.com › prisma › prisma › issues › 13851
A transaction rollback should go through Prisma middleware · Issue #13851 · prisma/prisma
June 16, 2022 - We use Prisma middleware to audit log database operations. If we're running those operations in a transaction, and the transaction rolls back, the middleware has no way to catch the rollback.
Author prisma
GitHub
github.com › prisma › prisma › issues › 15211
Support manual rollback in interactive transactions · Issue #15211 · prisma/prisma
September 6, 2022 - await prisma.$transaction( async (prisma) => { // Code running in a transaction... prisma.rollback(); // or with arbitrary data/error prisma.rollback(err); } )
Author prisma
Goprisma
goprisma.org › docs › walkthrough › transactions
Transactions – Prisma Client Go
// this will fail, since the record doesn't exist... a := client.Post.FindUnique( db.Post.ID.Equals("does-not-exist"), ).Update( db.Post.Title.Set("new title"), ).Tx() // ...so this should be roll-backed, even though itself it would succeed b := client.Post.FindUnique( db.Post.ID.Equals("123"), ).Update( db.Post.Title.Set("New title"), ).Tx() if err := client.Prisma.Transaction(b, a).Exec(ctx); err != nil { // this err will be non-nil and the transaction will rollback, // so nothing will be updated in the database panic(err) } Last updated on October 22, 2024 ·
Fixdevs
fixdevs.com › home › blog › fix: prisma transaction error — transaction already closed or rolled back
Fix: Prisma Transaction Error — Transaction Already Closed or Rolled Back - FixDevs
May 22, 2026 - The trade-off is a default timeout of 5 seconds. If the callback hasn’t completed by then, Prisma automatically rolls back the transaction and throws. This timeout exists because interactive transactions hold database locks for their entire duration, and a stuck transaction blocks other queries.
GitHub
github.com › prisma › prisma › issues › 5616
$transaction doesn't rollback in case any transaction fails · Issue #5616 · prisma/prisma
February 12, 2021 - const txn1 = this.prisma.table1.create({ data: { name: `Test 1` }, }); const txn2 = this.prisma.table1.create({ data: { name: undefined //`Test 2`, }, }); const transactionResults = await this.prisma.$transaction([txn1, txn2]); name is a mandatory column so when I send undefined, txn2 fails but txn1 still creates a record in the table even though it should rollback as txn2 failed.
Author prisma
Stack Overflow
stackoverflow.com › questions › 73179585 › using-prisma-and-transactions
typescript - Using Prisma and transactions? - Stack Overflow
await globals.prisma.$executeRawUnsafe("begin"); await globals.prisma.$executeRawUnsafe("... just random update"); //await globals.prisma.$executeRawUnsafe("rollback"); await globals.prisma.$executeRawUnsafe("commit");
Callgent
docs.callgent.com › blog › nestjs+prisma-transaction-propagation-and-test-rollback-and-tenancy
🔧 Nestjs + Prisma, transaction propagation & test rollback & multi-tenancy - Service as a Callgent
March 16, 2024 - ClsModule.forRoot({ plugins: [ new ClsPluginTransactional({ imports: [PrismaModule], // comment this line, if you have existing PrismaModule middleware: { mount: true }, // mount plugin as nestjs middleware adapter: new TransactionalAdapterPrisma({ // Prisma adaptor // the original PrismaService is wrapped into the adaptor prismaInjectionToken: PrismaService, }), }), ], }), More complicated use cases, please read it's documentation. After each test case, we need to rollback the transaction, preventing side-effects.