Skip to content

Interactive transactions: Allow setting isolation level #8668

Closed

Description

@dpetrick

Problem

The preview for interactive transactions (iTX) doesn't allow for setting the isolation level when starting the transaction. However, there are plenty of scenarios where setting the isolation level is required to make use of iTX.

Suggested solution

Allow for something like:

let opts = { isolation: `ReadUncommitted` }
await prisma.$transaction(opts, async (prisma) => {
    // ...
})

Alternatives

We decide to not support this at all.

Activity

  1. move-zig commented on Mar 20, 2022

    @move-zig

    I just wanted to say that this is an important feature.

    I'm using optimistic concurrency control in conjunction with $transaction calls (both array and functional types). Because MySQL/MariaDB default to the REPEATABLE READ transaction isolation level, there are times where OCC won't work correctly.

    const file = await prisma.file.findUnique(...);
    
    return prisma.$transaction(async transaction => {
      // update database
      const batchPayload = await transaction.file.updateMany({
        data: { ..., entityVersion: { increment: 1 } },
        where: { ..., entityVersion: file.entityVersion },
      });
    
      if (batchPayload.count === 0) {
        return false;
      }
    
      // write a file
      await fs.writeFile(...);
    
      return true;
    });

    If two requests enter the transaction around the same time, they will both see the old entity version and succeed with the updateMany. We need to use a transaction level like READ COMMITTED so that both requests see the latest version of the table, rather than a consistent snapshot created at the start of the transaction.

    Right now I'm doing this with await this.prisma.$executeRaw`SET TRANSACTION ISOLATION LEVEL READ COMMITTED`; just before I start the call to prisma.$transaction.

    SET TRANSACTION ISOLATION LEVEL READ COMMITTED sets the transaction isolation level for the next transaction within the current database session, and I'm not convinced that the "next transaction" is necessarily going to be the one I intend to start on the next line. Some other request could potentially start a transaction first. Since I'm using a single prisma object for the entire app (as per the docs), couldn't this other transaction be within the same database session and "steal" my isolation level?

    I'm hoping that something like the feature being proposed could ensure that the isolation level is used only for the intended transaction.

  2. self-assigned this
    on Jul 4, 2022
  3. garrensmith commented on Aug 12, 2022

    @garrensmith
    Contributor

    This functionality is now available in the latest version of Prisma.

  4. added this to the 4.2.0 milestone on Aug 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions