Skip to content

PrismaPG: prismaClient unusable after calling $disconnect since 6.6.0 #27157

@mnahkies

Description

@mnahkies

Bug description

Since the new driver adapter interface from 6.6.0, calling $disconnect calls end() on the underlying pg.Pool, and doesn't construct a fresh pool when new queries are issued.

This causes any subsequent queries to fail with an obtuse exception.

Severity

🔹 Minor: Unexpected behavior, but does not block development

Reproduction

import {PrismaPg} from "@prisma/adapter-pg";
import {PrismaClient} from "@prisma/client";
import {Pool} from "pg";

async function main() {
  // const pool = new Pool({
  //   connectionString: process.env.CONNECTION_STRING,
  //   max: 10,
  //   allowExitOnIdle: true,
  // });

  const pgAdapter = new PrismaPg({
    connectionString: process.env.CONNECTION_STRING,
    max: 10,
    allowExitOnIdle: true,
  });
  const prismaClient = new PrismaClient({
    log: [
      {
        emit: 'event',
        level: 'query',
      },
    ],
    adapter: pgAdapter,
  });

  console.info(await prismaClient.$executeRaw`SELECT 1`)
  await prismaClient.$disconnect()
  // on 6.5.0 this works, 6.6.0 and up it doesn't.
  console.info(await prismaClient.$executeRaw`SELECT 1`)
}

main()

Expected vs. Actual Behavior

Expected
The client should reconnect next time a query is issued.

Actual
An obtuse error is thrown, which if you add logging to

you'll find it's a error from pg saying the pool has already been ended.

PrismaClientKnownRequestError: 
Invalid `prisma.$executeRaw()` invocation:


Error in external connector (id 0)
    at zn.handleRequestError (/Users/michaelnahkies/projects/node-libs/node_modules/@prisma/client/runtime/library.js:121:7459)
    at zn.handleAndLogRequestError (/Users/michaelnahkies/projects/node-libs/node_modules/@prisma/client/runtime/library.js:121:6784)
    at zn.request (/Users/michaelnahkies/projects/node-libs/node_modules/@prisma/client/runtime/library.js:121:6491)
    at async l (/Users/michaelnahkies/projects/node-libs/node_modules/@prisma/client/runtime/library.js:130:9778)
    at async main (/Users/michaelnahkies/projects/node-libs/libs/database/build/junk.js:28:18) {
  code: 'P2036',
  meta: { id: 0 },
  clientVersion: '6.7.0'
}

Frequency

Consistently reproducible

Does this occur in development or production?

Only in production (e.g., query engine, generated client)

Is this a regression?

Yes, since 6.6.0

Workaround

Re-construct PrismaClient after any $disconnect.

Prisma Schema & Queries

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["views", "relationJoins", "prismaSchemaFolder", "driverAdapters"]
}

datasource db {
  provider = "postgresql"
  url      = "postgresql://..."
}

Prisma Config

  "prisma": {
    "schema": "./prisma"
  },

Logs & Debug Info

No response

Environment & Setup

  • OS: MacOS / Linux
  • Database: PostgresSQL
  • Node.js version: v22.13.1

Prisma Version

Prisma schema loaded from prisma
prisma                  : 6.7.0
@prisma/client          : 6.7.0
Computed binaryTarget   : darwin-arm64
Operating System        : darwin
Architecture            : arm64
Node.js                 : v22.13.1
TypeScript              : 5.7.3
Query Engine (Node-API) : libquery-engine 3cff47a7f5d65c3ea74883f1d736e41d68ce91ed (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Schema Engine           : schema-engine-cli 3cff47a7f5d65c3ea74883f1d736e41d68ce91ed (at node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm             : @prisma/prisma-schema-wasm 6.7.0-36.3cff47a7f5d65c3ea74883f1d736e41d68ce91ed
Default Engines Hash    : 3cff47a7f5d65c3ea74883f1d736e41d68ce91ed
Studio                  : 0.511.0
Preview Features        : driverAdapters, prismaSchemaFolder, relationJoins, views

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions