Too many instances of PrismaClient
See original GitHub issueBug description
I’m getting the classic FATAL: remaining connection slots are reserved for non-replication superuser connections
error for my Postgres DB because there are too many PrismaClient
instances going on.
Problem is, I’m using REST API routes (using NextJS) and not GraphQL, so each time there’s an incoming request to a route (and there are many), I’m instantiating a PrismaClient
, which results in the aforementioned error.
Is there a way to avoid this?
Environment & setup
- OS: macOS
- Database: PostgreSQL
- Node.js version: 12.6.0
- Prisma version:
2.11.0
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:22 (7 by maintainers)
Top Results From Across the Web
Best practice for instantiating PrismaClient with Next.js
Solution. The solution in this case is to instantiate a single instance PrismaClient and save it on the global object. Then we keep...
Read more >handle multiple instances prisma warning in nestjs testing
I'm writing tests for a Nestjs API. when I run it, it gives this error: warn(prisma-client) There are already 10 instances of Prisma...
Read more >How to fix the `Already 10 Prisma Clients are actively running ...
... and learn the fundamentals, HTML, CSS, JS, Tailwind, React, Next.js and much more! ... import { PrismaClient } from '@prisma/client' const prisma...
Read more >Building a REST API with NestJS and Prisma - YouTube
... closed captions for ANY video valuable to our community (for example, about: Node.js, TypeScript & Type Safety, Prisma, databases, etc).
Read more >Hi has anyone experienced the dreaded Error querying the dat ...
Error querying the database: db error: FATAL: sorry, too many clients already ... I found another example in the docs to cut down...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I understand how and why this works during development but why is the issue not present for production? Why is it okay to create a new prisma client each time while running in production?
Edit: I read some more and realize now that in a production setting the above solution gets cached the first time the module is loaded so you don’t run into the issue. Only during development does NextJS clear the cache for hot reloading purposes. Leaving comment in case others have same confusion.
Works like a charm, @vladandrei0.
module
API
e.g.
/api/payments/create
However, a drawback is that referencing Prisma Client indirectly stops the auto-completion (at least in VS Code). A workaround is to temporarily enable while creating the API:
and then to disable it once done: