question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unclear where to call prisma.disconnect()

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:14
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

10reactions
timsuchanekcommented, Mar 6, 2020

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 disconnect call for Prisma Client.

If the Node process exits for whatever reason (e.g. a crash), the client will also close the connection automatically.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connecting and disconnecting (Concepts) - Prisma
PrismaClient connects and disconnects from your data source using the following two methods: $connect() · $disconnect(). In most cases, you do not need...
Read more >
Should i disconnect my prisma client after every query in ...
When i tried use mongoose, I create some cache for the database to not create a connection everytime, but the same thing kept...
Read more >
I m running into an issue where I m hitting 20 20 connection | Prisma
prisma.$disconnect() method on all api endpoints, so i'm confused as to why this is an issue ... prisma client, and removed all of...
Read more >
How to write tests for Prisma with Docker and Jest
For unit tests, create a Prisma client and disconnect after each test. For functional tests, start a server and close it after each...
Read more >
Prisma Access Known Issues - Palo Alto Networks
Cortex Data Lake failed to reconnect after a disconnect if a ... that manages Prisma Access, and check that Panorama is able to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found