Skip to content

Client extensions that require use of a client-level method silently ignore transactions #20678

@fnimick

Description

@fnimick

Bug description

Any Prisma client extensions that require use of the client directly in order to call client-level methods will silently ignore transactions. This is not documented and should be (or be fixed), considering how many examples require or recommend the use of the client.

Example 1
Example 2
Example 3

In all of these cases, if you trigger the extension from inside a transaction (interactive or batched), the extension code will issue the queries in a new connection and ignore the transaction that you think you are operating in.

How to reproduce

  • Issue a query which mutates the database using one of the examples in a transaction, followed by one which deliberately errors
  • Confirm via the debug log that multiple connections and transactions are being used
  • Confirm that the database mutation, which should have been in the transaction and rolled back, is persisted

Expected behavior

There should be a way to write client extensions that can operate using the client or the current transaction context.

Prisma information

An example test using the RLS prisma extension example linked above:

  await rlsPrismaClient.$transaction(async (tx) => {
    console.log(await tx.organization.findMany());
    console.log("---");
    await tx.organization.create({ data: { name: "testOrg" } });
    console.log(await tx.organization.findMany());
    throw new Error("boom");
  });

The testOrg organization is created and persisted, even though the creation call was made on a transaction client context.

An alternative example using batched transactions, which fails in the same way:

  console.log(await rlsPrismaClient.$transaction([
    rlsPrismaClient.organization.findMany(),
    rlsPrismaClient.organization.create({ data: { name: "testOrg" } }),
    rlsPrismaClient.organization.findMany(),
    rlsPrismaClient.organization.create({ data: { name: "testOrg", id: 'foobar' } }), // id is deliberately invalid
  ]));

Environment & setup

  • OS: macOS
  • Database: PostgreSQL 14
  • Node.js version: 18.14.1

Prisma Version

prisma                  : 5.1.1
@prisma/client          : 5.1.1
Current platform        : darwin-arm64
Query Engine (Node-API) : libquery-engine 6a3747c37ff169c90047725a05a6ef02e32ac97e (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Schema Engine           : schema-engine-cli 6a3747c37ff169c90047725a05a6ef02e32ac97e (at node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm             : @prisma/prisma-schema-wasm 5.1.1-1.6a3747c37ff169c90047725a05a6ef02e32ac97e
Default Engines Hash    : 6a3747c37ff169c90047725a05a6ef02e32ac97e
Studio                  : 0.492.0
Preview Features        : multiSchema

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions