R
Railway3y ago
Kezi

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
JustJake
JustJake3y ago
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
Kezi
KeziOP3y ago
mmh it does close the connections as seen on the first screenshot (pgadmin) right? But yeah I should add that as well
Kezi
KeziOP3y ago
Kezi
KeziOP3y ago
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
JustJake
JustJake3y ago
Hmmm That is super weird
Kezi
KeziOP3y ago
tried removing 1 promise.all but it's still like 200 database connections so .. yeah
Kezi
KeziOP3y ago
Kezi
KeziOP3y ago
Shouldn't this be === 'production' ? so it uses the global instead of instantiating
nebula
nebula3y ago
yeah that would do it can you send that code snippet?
Kezi
KeziOP3y ago
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
nebula
nebula3y ago
wait are you running db queries client side
Kezi
KeziOP3y ago
No it's in getStaticProps
nebula
nebula3y ago
ah ok
Kezi
KeziOP3y ago
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
nebula
nebula3y ago
i see
Kezi
KeziOP3y ago
But I do have a "singleton" file
JustJake
JustJake3y ago
You should make it a singleton
Kezi
KeziOP3y ago
that should do the work
JustJake
JustJake3y ago
err
Kezi
KeziOP3y ago
isn't it one already tho? I just followed the prisma guide for next.js
nebula
nebula3y ago
no it spawns many processes
JustJake
JustJake3y ago
Only if you actually have a concept of "create or fetch this"
nebula
nebula3y ago
i think
Kezi
KeziOP3y ago
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:...
Kezi
KeziOP3y ago
Just tried this solution still 200 db connections??
Kezi
KeziOP3y ago
Kezi
KeziOP3y ago
gonna try with .freeze welp same thing.. Ok soooo
Kezi
KeziOP3y ago
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!
JustJake
JustJake3y ago
Welcome! Archiving
Want results from more Discord servers?
Add your server