Skip to content
Discussion options

You must be logged in to vote

This happens because Next.js reloads modules frequently in dev mode, and every reload creates a new PrismaClient instance — which overloads the database connection pool.

✅ Fix:
Use a singleton pattern so Prisma reuses the same client instead of reinitializing it on every reload.

Here’s the recommended setup 👇

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

const globalForPrisma = global as unknown as { prisma: PrismaClient };

export const prisma =
  globalForPrisma.prisma ||
  new PrismaClient({
    log: ["query"],
  });

if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;

export default prisma;

Then simply import in your code:

import prisma from "…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by trbgaming100
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Ask and answer questions about GitHub features and usage Programming Help Discussions around programming languages, open source and software development
3 participants