# Prisma connection pool exhaustion fixer

> Fixes database 'too many connections' crashes on serverless apps.

- **What it does:** Diagnoses and fixes 'too many database connections' crashes that happen when apps built with Prisma run on serverless hosting like Vercel or AWS Lambda, so your app stops timing out under traffic.
- **Agents:** Claude Code and Codex (through orbit-connect), Claude and ChatGPT (through the Orbit connector)
- **Install:** `npx -y --loglevel=error orbit-connect@0.3.33 install skl_34e3f5df9a619ca0ffd6bc1a`
- **Creator:** Mariah Brunner (@itsmariahbrunner) on Instagram
- **Safety:** Checked by Orbit: nothing risky found in what it runs or reads
- **Last updated:** 2026-11-14
- **Page:** https://orbitinbox.app/s/prisma-connection-pool-exhaustion-fixer

## What you'll need

- Supabase: the managed Postgres database whose connection limits the fix targets
- Vercel: the serverless host where the connection errors appear

## Skill

---
name: prisma-connection-pool-fix
description: Use when Prisma throws P2024 or "too many connections" on Vercel, Lambda or Netlify: set a pooled URL, connection_limit=1 and a singleton client.
---

# Fix Prisma connection pool exhaustion

## What this does
Fixes Prisma "Timed out fetching a new connection" and "too many connections" errors in serverless hosting. Each cold start creates a Prisma client that opens several connections (default 5), which quickly exhausts the database limit (often 20-100).

## When to use
- `P2024: Timed out fetching a new connection from the connection pool`
- PostgreSQL `FATAL: too many connections for role ...` or MySQL `Too many connections`
- Works with `npm run dev` locally but fails in production serverless
- Intermittent timeouts during traffic spikes; database dashboard near its connection limit

## How to run it
1. Confirm the environment: serverless host, Prisma, connection-based database (Supabase, Neon, RDS, etc.).
2. Use a connection pooler. Check the provider's docs for the exact URL. Examples from the source:
   - Supabase: use the pooled string (port 6543, not 5432) with `?pgbouncer=true`.
   - Neon: has built-in pooling; use its connection string with `sslmode=require`.
   - Prisma Accelerate: run `npx prisma generate --accelerate`.
3. Add URL parameters to the connection string (merge with existing query params, do not append a second `?`):
   `connection_limit=1&pool_timeout=20&connect_timeout=10`
   - `connection_limit=1`: one connection per serverless instance (start here, raise if latency suffers)
   - `pool_timeout=20`: wait up to 20s for a free connection
   - `connect_timeout=10`: fail fast if it can't connect
   Example shape: `DATABASE_URL="postgresql://{user}:{password}@{pooler-host}:6543/postgres?pgbouncer=true&connection_limit=1"`
4. Use one shared client file (prevents hot-reload from creating new clients in development):

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

const globalForPrisma = globalThis as unknown as {
  prisma: PrismaClient | undefined
}

export const prisma = globalForPrisma.prisma ?? new PrismaClient()

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


5. Import `prisma` from this file everywhere. Do not create new `PrismaClient` instances elsewhere.
6. Do not change `relationMode` in `schema.prisma` for this fix. It does not limit connections.

## Verify
- Deploy to production.
- Load test: `npx autocannon -c 100 -d 30 https://{your-app}/api/test`
- Check the database dashboard: connections stay within limits.
- Confirm no more P2024 errors in logs.

## Notes
- The singleton only helps in development. In production each serverless instance is isolated.
- Next.js API routes may each run as a separate serverless function.
- PlanetScale (MySQL) uses a different architecture and doesn't have this issue.
- Prisma Accelerate adds caching plus pooling.

## Output
Updated `DATABASE_URL` guidance (no real credentials in files or chat), the shared `lib/prisma.ts`, and a short verification report.

## Credit
Based on public content by blader, packaged by Orbit.

Shared by Mariah Brunner, credited on Orbit.