Next.js Discord

Discord Forum

do i have to disconnect prisma client

Unanswered
Bully Kutta posted this in #help-forum
Open in Discord
Bully KuttaOP
i have a api route that creates a new PrismaClient() everytime its ran and doesnt disconnect it
do i make it disconnect before returning data?
bit unrelated i know lol but idk where else to ask this

6 Replies

@Bully Kutta i have a api route that creates a new PrismaClient() everytime its ran and doesnt disconnect it do i make it disconnect before returning data? bit unrelated i know lol but idk where else to ask this
normaly your ORM (prisma in this case) opens a connection to your database. You should close this connections after you are done with the sql query. Else you may hit the too many clients error and you wont be able to query your database anymore. So make sure you either close them directly manually after you done or automatically via a idle timeout for example
@Bully Kutta so when i return data PrismaCleint doesnt automatically close the connection?
it looks like you can directly disconnect it after your query is done. As said: you can either close them manually (https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/databases-connections/connection-management#disconnect) or automatically by setting up a idle timeout
Bully KuttaOP
thank u