5
How do I share a Prisma client across Next.js server actions without exhausting connections?
MPAsked by Mina Park · 15d
In dev I keep getting too many connections after hot reloads. What's the right pattern?
In dev I keep getting too many connections after hot reloads. What's the right pattern?
Accepted answer
Cache the client on globalThis in development:
const g = globalThis as any;
export const db = g.prisma ?? new PrismaClient();
if (process.env.NODE_ENV !== 'production') g.prisma = db;Hot reload re-imports modules, which creates new clients; the global keeps one.
Also consider a pooler like PgBouncer in production if you're on serverless.
Sign in to answer.