Unclear where to call prisma.disconnect()
See original GitHub issueI read this section on managing connections in your docs and I found it a bit confusing.
To start, the important note is not very clear… I don’t understand what the scenario is in which the Prisma Client doesn‘t do the right thing. A better explanation and a code snippet showing the problem would be really helpful.
Additionally, the important note simultaneously says that the Prisma Client should just do the right thing but also says that sometimes it won’t. I think it should be one or the other. If the client can be made such that it always does the right thing, then great! If not, don’t even mention it, just clearly explain what someone has to do to protect themselves.
Which leads into the last problem… which is that I don’t know where to call prisma.disconnect(). Right now I’m just peppering disconnects after every query—that’s clearly not right.
I’m using Express, so… would the generally correct thing be to use a middleware to disconnect at the end of the request/response cycle?
import { PrismaClient } from "@prisma/client"
const prisma = new PrismaClient()
express.use(function(req, res, next) {
res.on("finish", () => prisma.disconnect())
next()
})
I took a look at the Prisma Client repo but because of the way it’s built I can’t tell if when I instantiate a new PrismaClient is it returning the same instance (a singleton) or am I getting a new instance? Rather, it’s unclear if calling prisma.disconnect() in this way would actually disconnect all connections, or just the connections that were specifically started with this instance of the client.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:14
- Comments:8 (5 by maintainers)

Top Related StackOverflow Question
Thanks for the question @brandonweiss! prisma/prisma#540 will not help with your situation, as it would only disconnect if Node would exit.
But as you have an Express server running, Node wouldn’t even exit.
In your case, as the Express server is running, you can just skip the
disconnectcall for Prisma Client.If the Node process exits for whatever reason (e.g. a crash), the client will also close the connection automatically.
Now documented: https://www.prisma.io/docs/concepts/components/prisma-client/working-with-prismaclient/connection-management#calling-disconnect-explicitly