Hey folks. I'm using Prisma on serverless with Nex...
# orm-help
m
Hey folks. I'm using Prisma on serverless with Next.js/Vercel and noticing what I'm quite sure are idle zombie connections that are taking over my database.
m
Copy code
import { PrismaClient } from '@prisma/client'

let prisma: PrismaClient

const g = global as any

if (process.env.NODE_ENV === 'production') {
  prisma = new PrismaClient()
} else {
  if (!g.prisma) {
    g.prisma = new PrismaClient()
  }

  prisma = g.prisma
}

export default prisma
are u sure u are using the same client isntead of invoking the new one each time?
d
Hey again Matt 👋, Which errors did you get suggesting you have idle zombie connections? When we speak about zombie connections, we mean connections that connections from the serverless function to the DB that are hanging after the function has executed. In your case, hanging “zombie” connections would be between the serverless function and PgBouncer. But the whole point of PgBouncer is to allow handling a lot more connections than the DB can handle. So it’s hard to say without more information what is actually the problem. Some ideas that might help resolve this: • Decreasing the timeout on PgBouncer (
client_idle_timeout
) to drop idle client connections. By default it’s disabled. • Calling
prisma.$disconnect
at the end of a Lambda functions. This should reduce the number of idle connections, however it comes at the cost of the connection overhead to PgBouncer when a warm function is reused and has to re-establish a connection to PgBouncer.