0

I am using Apollo Server Express and Prisma ORM on my backend. I have created just one prisma instance and put it on graphql context and i am using this prisma instance on my resolvers.

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();
const server = new ApolloServer({
    ...
    ...
    playground: true,
    context: ({req, res}) => ({ req, res, prisma }),
  });

Just creating a instance. But when frontend starts work, prisma connection count is incrementing and after a while, PostgreSQL giving too many connections error, I verified this error from PgAdmin.

bomi
  • 113
  • 2
  • 11
  • [prisma connection pool](https://www.prisma.io/docs/concepts/components/prisma-client/working-with-prismaclient/connection-pool#setting-the-connection-pool-size) also tried this but still same. i will observe this. – bomi Aug 03 '21 at 22:39
  • There were some [similar issues](https://github.com/prisma/prisma/issues/1983) with people using NestJS as you described. Could you clarify if this is happening in localhost or in production/staging? Additionally, please try these two things: 1. Try setting ```connection_pool=1``` and see if it helps. Finally, could you check your code for any other accidental prismaClient initializations? – Tasin Ishmam Aug 04 '21 at 12:46
  • It is happening in production. I seperated the prisma instance to own prisma file. Still same error Error querying the database: db error: FATAL: remaining connection slots are reserved for non-replication superuser connections – bomi Aug 05 '21 at 08:03
  • Super sorry to hear this is persisting. Would you kindly open a [new issue](https://github.com/prisma/prisma/issues/new/choose) on GitHub with details about your environment? Bonus points if you could provide some kind of reproducable code. We try our best to track and prioritize bugs on Github and so we might be better able to help address this over there. – Tasin Ishmam Aug 05 '21 at 21:47

1 Answers1

1

I found a way to do this in Nextjs:

// lib/prisma.ts
import { PrismaClient } from '@prisma/client';

let prisma: PrismaClient;

if (process.env.NODE_ENV === 'production') {
  prisma = new PrismaClient();
} else {
  if (!global.prisma) {
    global.prisma = new PrismaClient();
  }
  prisma = global.prisma;
}

export default prisma;

Here: https://vercel.com/guides/nextjs-prisma-postgres Maybe it could help

Ender Bonnet
  • 128
  • 1
  • 7