prisma: Prisma client can't reach database server from time to time

Bug description

Hi,

We are running a very classic NodeJS Express API connected to a PostgreSQL database via the Prisma client, and from time to time we have requests failing with the error P1001 :

Can't reach database server at ...
Please make sure your database server is running at ...

And everything is ok in the previous or following requests few milliseconds apart:

Capture d’écran 2022-03-13 à 16 46 24

I checked on the infrastructure side, and the database seems reachable from the server with no issues of connection pool, overload, high CPU, high memory or whatever else.

Is there something I can check or try to solve that ?

How to reproduce

I didn’t find any way to reproduce it constantly but it seems to happen a little bit more during low traffic periods like night. And it seems that sending queries every 20 seconds reduce a lot the issue. So it’s maybe linked to idle timeout or something similar.

Expected behavior

No more requests failing with this error of course 😅 ! I don’t expect a fix or whatever, but more at advices or things I can take a look to to move forward in solving this problem.

Prisma information

It happens with different queries, we even have very basic health checks failing from time to time due to this error:

export default async function () {
  await client.$queryRaw`SELECT 0`;
}

Environment & setup

  • OS: Ubuntu
  • Database: PostgreSQL
  • Node.js version: 16.13.0

Prisma Version

3.10.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 31 (11 by maintainers)

Most upvoted comments

Because this have a high SEO, I would add that it is important to verify if you are dealing correctly with a lot of connections on the database connection pool. Make sure to use disconnect method await prisma.$disconnect(); after you finish your query or mutation.

I am also facing the same issue, prisma sometime connects to postgres DB correctly and someimes not. I tested it on 2 providers( render, heroku) but I am still getting this problem. It normally occurs while calling findMany function.

the error thrown is: Can’t reach database server at … Please make sure your database server is running at …

Prisma version: 4.5.0

I’m facing the following error “sometimes”, is there a way to automagically handle this error and reconnect?

PrismaClientKnownRequestError:
Invalid prismaClient_1.default.flight.findUnique() invocation in
/dist/database/flight.js:24:64

21 /
22 function getFlightInfo(flightId) {
23 return __awaiter(this, void 0, void 0, function () {
→ 24 const flightInfo = yield prismaClient_1.default.flight.findUnique(
Can't reach database server at ************.rds.amazonaws.com:3306

Please make sure your database server is running at ************.rds.amazonaws.com:3306.
at Zr.handleRequestError (/node_modules/@prisma/client/runtime/library.js:171:6414)
at Zr.handleAndLogRequestError (/node_modules/@prisma/client/runtime/library.js:171:5948)
at Zr.request (/node_modules/@prisma/client/runtime/library.js:171:5786)
at async t._request (/node_modules/@prisma/client/runtime/library.js:174:10455) {
code: 'P1001',
clientVersion: '4.11.0',
meta: {
database_host: '************.rds.amazonaws.com',
database_port: 3306
}
}

Hi @pantharshit00 ,

Yes I noticed there was several fixes / improvements of the connection in the last Prisma client versions, many thanks 🙏🏻 !

We also did some changes on our side:

  • opened support ticket to our hosting provider
  • add a health check to the connection between our API and the databases
  • upgraded to the latest versions of the Prisma client

Consequently the errors rate drops from 10 per days to 10 during the last month.

So it’s better 👍🏻 even if I can’t say it’s clearly linked to the Prisma client upgrade.

But is that related to your original issue here ?

Not exactly, but with this fix, in case of error I could try to disconnect / reconnect the client and retry the query. Without the fix if I try to reconnect and it fails the client is completely stuck

This is explicitly when trying to establish a new connection to the DB, not when attempting to get a connection from Prisma’s pool right?

The way you ask makes me scared to answer @drewcorlin1 😆 But yes, fundamentally you should get a different answer for the connection pool, a connection pool timeout or other message that mentions the connection pool.