Too many connections to database but I followed prisma guide for Next.js
Hey, I'm having (again) an issue with multiple connections being created by Prisma to the pgsql database. I tried following multiple solutions including the one on the Prisma docs but it doesn't seem to work
Am I missing something really important?
Thank you for any help!!
31 Replies
This usually happens on localhost or in a serverless environment
Main thing is that you're not closing the connections
You'll likely need to add a process handler for exits
mmh it does close the connections as seen on the first screenshot (pgadmin) right?
But yeah I should add that as well
It never happened on localhost, only while building the app
And it worked fine yesterday, I made some changes using Promise.all and then it wasn't happy
Hmmm
That is super weird
tried removing 1 promise.all
but it's still like 200 database connections so .. yeah
Shouldn't this be === 'production' ?
so it uses the global instead of instantiating
yeah
that would do it
can you send that code snippet?
I mean, it's worse with Promise.all
since it does more requests and create more connections I think?
https://github.com/NieR-Rein-Guide/nierrein.guide/blob/main/src/pages/characters/%5B%5B...costume%5D%5D.tsx#L170
(Yeah there is probably a more efficient way to do this as well)
but if there is only 1 connection it should work just fine I think? I have no issue locally
wait
are you running db queries client side
No it's in getStaticProps
ah ok
it is run in the build phase
problem issue I have a lot of queries on multiple pages
So it generates a lot of clients
i see
But I do have a "singleton" file
You should make it a singleton
that should do the work
err
isn't it one already tho?
I just followed the prisma guide for next.js
no it spawns many processes
Only if you actually have a concept of "create or fetch this"
i think
GitHub
For those having this issue, I solved it by creating a singleton:...
For those having this issue, I solved it by creating a singleton: import { PrismaClient } from '@prisma/client' class DBClient { public prisma: PrismaClient private static instance:...
Just tried this solution
still 200 db connections??
gonna try with .freeze
welp same thing..
Ok soooo
apparently appending
?schema=public&connection_limit=1
to DATABASE_URL is working
but it still makes 100 connections? lol
Also I thought the build would be longer since there should be only 1 connection, but it was actually a lot faster
Curious
Thanks for your help!Welcome! Archiving