Skip to content

Unclear where to call prisma.disconnect() #648

@brandonweiss

Description

@brandonweiss

I 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.

Metadata

Metadata

Labels

type/docsDocumentation creation, updates or corrections

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions