P
Prisma3mo ago
LuCrypto

Prisma too many connection to my database

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
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
9 Replies
jonfanz
jonfanz3mo ago
How are you deploying your application?
LuCrypto
LuCrypto3mo ago
Im using vercel for deploy my app ! Maybe because vercel it's serverless so there is a problem ? But i have the same problem in local
jonfanz
jonfanz3mo ago
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
Connection pooling - Neon Docs
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...
LuCrypto
LuCrypto3mo ago
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?
jonfanz
jonfanz3mo ago
Not sure. How are you importing the Prisma Client in other parts of your application?
LuCrypto
LuCrypto3mo ago
import { prisma } from "@/src/lib/prisma"; and i put prisma.ts just above
jonfanz
jonfanz3mo ago
Right. Just comparing that against our documentation.
jonfanz
jonfanz3mo ago
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
LuCrypto
LuCrypto3mo ago
Ok thanks, i'll take a look !
Want results from more Discord servers?
Add your server
More Posts