P
Prisma2mo 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
9 Replies
Nurul
Nurul2mo ago
Hey 👋 Do you mind sharing your GitHub handle with me so that I can take a look at logs from our end?
Hinky
Hinky2mo ago
GitHub
TheHinky - Overview
GitHub is where TheHinky builds software.
Hinky
Hinky2mo 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
Hinky2mo 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
Hinky2mo 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
Nurul2mo 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
Hinky4w 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
Nurul4w ago
No worries! 🙏
Hinky
Hinky4w ago
Just a small update, its up to about 60 connnections now.