P
Prisma2y ago
Ahmad

To many database connections ope

Hey, so im working on a nextjs app router. When deploying the app it says, Too many connection opend. Yes im using prisma client best practice, and yes after every connection im using prisma.disconnect. also yes im using more that one connection in one place but im using await and after every query im disconnecting it, so what can i do
No description
14 Replies
Ahmad
AhmadOP2y ago
This is the way im using it
No description
Ahmad
AhmadOP2y ago
I forget to mention that the when deploying the dashboard it works just fine. But when getting the data from the api thro another project using axios or fetch, this happens
Ahmad
AhmadOP2y ago
This js how im getting the data from the user project
No description
Ahmad
AhmadOP2y ago
So is there any solution? Max connection is passing the 5 users
Enzonaki
Enzonaki2y ago
Why are you disconnecting after every single query :6746_52_CryCat: Use a single PrismaClient, and set the connection pool manually But please, for the love of God and all that is holy, delete all these disconnects That is absolutely the work of evil
Enzonaki
Enzonaki2y ago
Ahmad
AhmadOP2y ago
😂😂😂😂 I'll try it So in my connection url I should add connection_limit=5 ? I added all those disconnected querys so It don't say to many connection, at the beginning it worked. But then when the app become much big the problem happened again
Ahmad
AhmadOP2y ago
I did this
No description
Ahmad
AhmadOP2y ago
I even set a timeout for 2 secs for each get operation to not face this problem but didn't work Whaatt Connection limit worked Aint no waaay letsgooooooooooooooooo Ok so are u sure i should delete all the discount? I will delete them but I swearto god if my app broke ill use 5 prisma.disconnect in every single query.....🌞😂 Can you please in a very quick and basic way explain about pooling and time out =2 or something idk and what do they do
DevPanda
DevPanda2y ago
show us the file where you export prisma
Ahmad
AhmadOP2y ago
No description
Ahmad
AhmadOP2y ago
I did the connection limit thing, i guess it worked but idk is it supposed to be this way?
DevPanda
DevPanda2y ago
I use this and I have never had to use disconnect myself. No matter how many users have used it
import { PrismaClient } from "../../prisma/generated/client";

const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient | undefined
}

const prisma = globalForPrisma.prisma ?? new PrismaClient()
export default prisma

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
import { PrismaClient } from "../../prisma/generated/client";

const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient | undefined
}

const prisma = globalForPrisma.prisma ?? new PrismaClient()
export default prisma

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
Ahmad
AhmadOP2y ago
I'll try it thank u very much The import of the prisma client is coming from where? Unfortunately didn't work. I have to use prisma.disconnect() everytime Idk how but using postgrasql solved the issue. Mysql is just 👎🏿

Did you find this page helpful?