P
Prisma6mo ago
Hinky

too many db connections on Prisma Accelerate

Hello! hope you are all well. I am using prisma accelerate to avoid this issue, but funny enough im running into it now im using accelerate (wasnt before i switch to prisma accelerate, (all just dev, not production yet)) I attached image of the error im running into. I for sure have prisma accelerate connected correctly, today i did like 30k queries. So my initial thought is the prisma singleton i have in my monorepo isnt working with hot reloading, but i still assumed should be okay because shouldnt be connecting directly, unless i do prisma migrate or similar, but i havent used that today. This is my current prisma singleton, but remember its in monorepo (turbo), idk if that changes anything.
import { PrismaClient } from "@prisma/client/edge";
// norm imports
import { withAccelerate } from "@prisma/extension-accelerate";

// refers to this
// https://github.com/prisma/accelerate-nextjs-starter/blob/main/lib/db.ts
//
const prismaClientSingleton = () => {
return new PrismaClient({
log:
// process.env.NODE_ENV === "development"
// ? ["query", "error", "warn"]
// : ["error"],
process.env.NODE_ENV === "development"
? ["error", "warn"]
: ["error"],
// process.env.NODE_ENV === "development" ? ["error"] : ["error"],
}).$extends(withAccelerate());
};

type tPrismaClientSingleton = ReturnType<typeof prismaClientSingleton>;

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

export const prisma = globalForPrisma.prisma ?? prismaClientSingleton();

if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
import { PrismaClient } from "@prisma/client/edge";
// norm imports
import { withAccelerate } from "@prisma/extension-accelerate";

// refers to this
// https://github.com/prisma/accelerate-nextjs-starter/blob/main/lib/db.ts
//
const prismaClientSingleton = () => {
return new PrismaClient({
log:
// process.env.NODE_ENV === "development"
// ? ["query", "error", "warn"]
// : ["error"],
process.env.NODE_ENV === "development"
? ["error", "warn"]
: ["error"],
// process.env.NODE_ENV === "development" ? ["error"] : ["error"],
}).$extends(withAccelerate());
};

type tPrismaClientSingleton = ReturnType<typeof prismaClientSingleton>;

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

export const prisma = globalForPrisma.prisma ?? prismaClientSingleton();

if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
any help would be awesome, thank you! also if anyone is from philippines, id love some dev help on fulltime basis (just chucking that out there too)
No description
12 Replies
Nurul
Nurul6mo ago
Hey 👋 Do you mind sharing your GitHub handle with me so that I can take a look at logs from our end?
Hinky
HinkyOP6mo ago
GitHub
TheHinky - Overview
GitHub is where TheHinky builds software.
Hinky
HinkyOP6mo ago
Only other thing i am thinking of, but i cant find information to confirm this. I am using prisma on edge, but also still have some lambda functions, as at the moment i am just using the same prismaClient edge for both edge and lamdba. I am thinking i should create two separate prisma singletons for each enviroment?
Hinky
HinkyOP6mo ago
Just a quick add on to this: current db limit is 100 connections. 90% of connections are coming from one ip, which im guessesing is prisma accerlate because its not any ip i have under control.
No description
Hinky
HinkyOP6mo ago
@Nurul (Prisma) I am sorry to @ you, i was just hoping if you had any sort of update from your end? i am needing to kill all connections often just for myself testing basically.
Nurul
Nurul6mo ago
Hey! Can you let me know if you have added any connection_limit parameter in your database connection string as mentioned here: https://www.prisma.io/docs/accelerate/connection-pooling#configuring-the-connection-pool-size
Accelerate: Connection Pooling | Prisma Documentation
Learn about everything you need to know to use Accelerate's connection pooling.
Hinky
HinkyOP6mo ago
thank you for the reply. Ive increase the db connction limit which helped. im guessing prisma accerlate defaults to 100 connections. which is all good just was causing issues iwth my dbeaver and so on connections. Ive done the conneciton limit now, set to 10. however prisma is still using over 10 connections, currnetly 19. but for now its good enough and will keep an eye on it. thank you nurul
Nurul
Nurul5mo ago
No worries! 🙏
Hinky
HinkyOP4mo ago
Just a small update, its up to about 60 connnections now. @Nurul (Prisma) sorry i am tagging you. i sent a db conneciton limit in string, i believe i set 30. My max connections is 200 on db. However prisma accerlate alone is taking up 102 connections, i am quite sure that this stage that prisma accerelate is going to run into conneciton limit. Are you able to have a look on your end? otherwise i think im better off going else where for pooling
Tauhoo
Tauhoo4mo ago
Do your connections go over the limit? I'm facing a similar issue. I don't know if they are related. https://discord.com/channels/937751382725886062/1269957438497493042
Nurul
Nurul4mo ago
@Hinky I see you have 4 projects. Can you let me know in which project and environment are you facing this? Is it in project named "ie-live"?
Hinky
HinkyOP4mo ago
yeah that is correct. its okay, im in middle of moving to neon db serverless adaptor, seems to have fixed it. yeah that well over. I am going to neon db with their serverless adaptor and seems to be going well so far.
Want results from more Discord servers?
Add your server