question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

$transaction doesn't roll back in NestJS when we pass method from external service

See original GitHub issue

Bug 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

Conversation from Public Slack here

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
timsuchanekcommented, Feb 18, 2021

Thanks @revivalme for the report. We’ll have a look at this!

0reactions
williamluke4commented, Mar 22, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found