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.

Too many instances of PrismaClient

See original GitHub issue

Bug 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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:22 (7 by maintainers)

github_iconTop GitHub Comments

14reactions
OliverGilancommented, Jun 11, 2021

Works like a charm, @vladandrei0.

module

import { PrismaClient } from '@prisma/client'

// See here: https://github.com/prisma/prisma-client-js/issues/228#issuecomment-618433162
let prisma

if (process.env.NODE_ENV === 'production') {
	prisma = new PrismaClient()
}
// `stg` or `dev`
else {
	if (!global.prisma) {
		global.prisma = new PrismaClient()
	}

	prisma = global.prisma
}

export default prisma

API

e.g. /api/payments/create

import prisma from 'lib/utils/prisma'

const ApiPaymentsCreate = async (req, res) => {
  ...
  return await prisma.payment.create({...})
}

export default ApiPaymentsCreate

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:

// import prisma from 'lib/utils/prisma'
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

and then to disable it once done:

import prisma from 'lib/utils/prisma'

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.

7reactions
heymartinadamscommented, Dec 21, 2020

Works like a charm, @vladandrei0.

module

import { PrismaClient } from '@prisma/client'

// See here: https://github.com/prisma/prisma-client-js/issues/228#issuecomment-618433162
let prisma

if (process.env.NODE_ENV === 'production') {
	prisma = new PrismaClient()
}
// `stg` or `dev`
else {
	if (!global.prisma) {
		global.prisma = new PrismaClient()
	}

	prisma = global.prisma
}

export default prisma

API

e.g. /api/payments/create

import prisma from 'lib/utils/prisma'

const ApiPaymentsCreate = async (req, res) => {
  ...
  return await prisma.payment.create({...})
}

export default ApiPaymentsCreate

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:

// import prisma from 'lib/utils/prisma'
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

and then to disable it once done:

import prisma from 'lib/utils/prisma'
Read more comments on GitHub >

github_iconTop 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 >

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