$transaction doesn't roll back in NestJS when we pass method from external service
See original GitHub issueBug description
$transaction works incorrectly in NestJS when we pass method from external service, but works well if it uses method from current service. It doesn’t roll back as expected.
Example:
// UserService
@Injectable()
export class UserService {
  constructor(private readonly prismaService: PrismaService) {}
  addMoney(userId: number, value: number) {
    return this.prismaService.user.update({
      where: { id: userId },
      data: {
        money: {
          increment: value,
        },
      },
    });
  }
}
// InventoryService
@Injectable()
export class InventoryService {
  constructor(
    private readonly prismaService: PrismaService,
    private readonly userService: UserService,
  ) {}
  async sell(userId: number, itemId: number) {
    const write1 = this.prismaService.userItem.delete({
      where: { ... },
    });
    // external service method
    const write2 = this.userService.addMoney(...);
    await this.prismaService.$transaction([write1, write2]);
  }
  addMoney(userId: number, value: number) {
    return this.prismaService.user.update({
      where: { id: userId },
      data: {
        money: {
          increment: value,
        },
      },
    });
  }
}
If write2 will fail - write1 doesn’t rollback. But… rollback will work as expected if write2 refers to the current service method. (this.addMoney)
How to reproduce
https://github.com/revivalme/nestjs-prisma-transaction-test Bug reproduction section
Expected behavior
Transaction write1 should rollback as transaction write2 failed.
Prisma information
Environment & setup
- OS: Windows 10
 - Database: SQLite, PostgreSQL
 - Node.js version: v14.15.4
 - Prisma version:
 
prisma               : 2.17.0
@prisma/client       : 2.17.0
Current platform     : windows
Query Engine         : query-engine 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules\@prisma\engines\query-engine-windows.exe)
Migration Engine     : migration-engine-cli 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine : introspection-core 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary        : prisma-fmt 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Studio               : 0.353.0
Issue Analytics
- State:
 - Created 3 years ago
 - Comments:6 (5 by maintainers)
 
Top Results From Across the Web
how to use transaction across service in nestjs with typeorm
I try my code as below but the transaction doesn't rollback while exception threw. The following code will rollback as expected, but I...
Read more >The Most Convenient Ways of Writing Transactions Within the ...
If you have some troubles with transactions in Nest.js - this article ... the entire transaction will be rolled back with all the...
Read more >Creating a Transaction Interceptor Using Nest.js
With this code, any error thrown in the controller will cause the transaction to roll back and data will return to its original...
Read more >API with NestJS #15. Defining transactions with PostgreSQL ...
Another important command here is ROLLBACK. With it, we can abort the current transaction. It discards all updates made by the transaction.
Read more >Handling Transactions in TypeORM and Nest.js With Ease
It begins, does some database changes, and then either is committed or, in case of any failure, is rolled back. This way database...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thanks @revivalme for the report. We’ll have a look at this!
Hey @revivalme, I’m going to close this issue, if you feel like the problems you are experiencing are still related to this specific issue then feel free to reopen it explaining why.