#Prisma too many connection to my database

13 messages · Page 1 of 1 (latest)

hoary ermine
#

Hey everyone,

I use Prisma for ORM and Neon for my database
I noticed something, my connections aren't unique and after some time, i reach to the max to my database

But i throught i'd solved the problem by doing this :

I have this file and i only imported prisma from this file in my project
prisma.ts

import { PrismaClient } from '@prisma/client'

const prismaClientSingleton = () => {
  return new PrismaClient()
}

declare global {
  var prismaGlobal: undefined | ReturnType<typeof prismaClientSingleton>
}

export const prisma = globalThis.prismaGlobal ?? prismaClientSingleton()

if (process.env.NODE_ENV !== 'production') globalThis.prismaGlobal = prisma

Why do I have so many connections?
Thanks in advance for your help

vapid shellBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

viral moat
#

How are you deploying your application?

hoary ermine
#

Maybe because vercel it's serverless so there is a problem ?
But i have the same problem in local

viral moat
#

yup yup, I assumed Next.js + Vercel.

If you are using serverless or edge functions on Vercel, then you will definitely run into issues. I'd recommend checking out the connection pooling docs from Neon. It's pretty straightforward.

Neon

Neon uses PgBouncer to offer support for connection pooling, enabling up to 10,000 concurrent connections. PgBouncer is a lightweight connection pooler for Postgres. This topic describes Neon's defaul...

hoary ermine
#

I think that when I put the api on Vercel, it creates server less functions.

#

But here, for example, just locally, so without vercel, I have a problem because I have too much connection
Where could this be coming from?

viral moat
#

Not sure. How are you importing the Prisma Client in other parts of your application?

hoary ermine
#

import { prisma } from "@/src/lib/prisma";

#

and i put prisma.ts just above

viral moat
#

Right. Just comparing that against our documentation.

#

I know that Next.js does some stuff to mimic Vercel's deployed environment, especially when it involves edge functions. It could be the same with serverless functions, which could cause a separate Prisma Client to be instantiated for each "instance" of that serverless function.

Using connection pooling would resolve your issue regardless of the location. We also have specific Neon documentation here