FeroxCloudJoin free
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?

2 answers

4

Accepted answer

Cache the client on globalThis in development:

typescript
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.

LFAnswered by Lena Fischer · 15d
3

Also consider a pooler like PgBouncer in production if you're on serverless.

ADAnswered by Amara Diallo · 15d

Sign in to answer.