PrismaP
Prisma8mo ago
5 replies
Masato

Server has closed the connection

Thank you for your continued support.

I am having trouble with “server has closed the connection.
Prisma does not perform a connection health check after creating a connection, right?
If the socket is closed due to TCP Idle Timeout in the network configuration or OS, rather than in the application or Prisma client, Prisma client will continue to hold the connection.

As a result, an error “server has closed the connection” occurs when executing a query, and the query cannot be executed.

Question
- Is there a way to periodically check if the connection is available on the Prisma client side? Something like this (https://github.com/prisma/prisma/issues/13264#issuecomment-1633258460)
- Is query retry processing necessary in this case? Like this (https://github.com/prisma/prisma/discussions/24490#discussioncomment-9743866)


The actual flow is as follows.

sequenceDiagram
  participant pc as Prisma Client
  participant cr as Cloud Run
  participant ds as DB Server

  pc ->> cr: Execute connect()
  cr ->> ds: Establish TCP connection
  Note over pc,ds: Connection is established, but remains idle afterward

  pc ->> pc: Idle state (for a long period)

  Note over ds: DB server has no idle timeout  
  ds -->> ds: Periodically sends TCP Keepalive

  ds ->> cr: TCP Keepalive check
  cr -->> cr: ⚠️ No response (connection was already dropped due to idleness)

  ds ->> cr: No Keepalive response → assumes disconnection
  ds -->> ds: ⛔ Marks as "Connection reset by peer"
  ds ->> cr: Closes the connection

  pc ->> ds: Sends a delayed query (SELECT * FROM ..)
  ds -->> pc: ⛔ Error response: "Server has closed the connection"


Thank you.
GitHub
Problem I need to check if the app is still connected to the DB. The solution must be universal for every database. Suggested solution Enable the "provider" field as a public (or getProvi...
DB connection health check · Issue #13264 · prisma/prisma
GitHub
Question Possibly related to this issue, but I'm confused as to how applications are supposed to gracefully handle transient DB connection errors due to things like database/pgbouncer restarts....
How am I supposed to retry P1017: Server has closed the connection ...
Was this page helpful?