Agent skill

prisma-connection-pool-exhaustion

Fix Prisma "Too many connections" and connection pool exhaustion errors in serverless environments (Vercel, AWS Lambda, Netlify). Use when: (1) Error "P2024: Timed out fetching a new connection from the pool", (2) PostgreSQL "too many connections for role", (3) Database works locally but fails in production serverless, (4) Intermittent database timeouts under load.

Stars 2,196
Forks 14

Install this agent skill to your Project

npx add-skill https://github.com/blader/Claudeception/tree/main/examples/prisma-connection-pool-exhaustion

SKILL.md

Prisma Connection Pool Exhaustion in Serverless

Problem

Serverless functions create a new Prisma client instance on each cold start. Each instance opens multiple database connections (default: 5 per instance). With many concurrent requests, this quickly exhausts the database's connection limit (often 20-100 for managed databases).

Context / Trigger Conditions

This skill applies when you see:

  • P2024: Timed out fetching a new connection from the connection pool
  • PostgreSQL: FATAL: too many connections for role "username"
  • MySQL: Too many connections
  • Works fine locally with npm run dev but fails in production
  • Errors appear during traffic spikes, then resolve
  • Database dashboard shows connections at or near limit

Environment indicators:

  • Deploying to Vercel, AWS Lambda, Netlify Functions, or similar
  • Using Prisma with PostgreSQL, MySQL, or another connection-based database
  • Database is managed (PlanetScale, Supabase, Neon, RDS, etc.)

Solution

Step 1: Use Connection Pooling Service

The recommended solution is to use a connection pooler like PgBouncer or Prisma Accelerate, which sits between your serverless functions and the database.

For Supabase:

# .env
# Use the pooled connection string (port 6543, not 5432)
DATABASE_URL="postgresql://user:pass@db.xxx.supabase.co:6543/postgres?pgbouncer=true"

For Neon:

# .env  
DATABASE_URL="postgresql://user:pass@ep-xxx.us-east-2.aws.neon.tech/dbname?sslmode=require"
# Neon has built-in pooling

For Prisma Accelerate:

bash
npx prisma generate --accelerate

Step 2: Configure Prisma Connection Limits

In your schema.prisma:

prisma
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
  // Limit connections per Prisma instance
  relationMode = "prisma"
}

In your connection URL or Prisma client:

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

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

export const prisma = globalForPrisma.prisma || new PrismaClient({
  datasources: {
    db: {
      url: process.env.DATABASE_URL + '?connection_limit=1'
    }
  }
})

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

Step 3: Singleton Pattern (Development)

Prevent hot-reload from creating new clients:

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

Step 4: URL Parameters

Add these to your connection string:

?connection_limit=1&pool_timeout=20&connect_timeout=10
  • connection_limit=1: One connection per serverless instance
  • pool_timeout=20: Wait up to 20s for available connection
  • connect_timeout=10: Fail fast if can't connect in 10s

Verification

After applying fixes:

  1. Deploy to production
  2. Run a load test: npx autocannon -c 100 -d 30 https://your-app.com/api/test
  3. Check database dashboard—connections should stay within limits
  4. No more P2024 errors in logs

Example

Before (error under load):

[ERROR] PrismaClientKnownRequestError:
Invalid `prisma.user.findMany()` invocation:
Timed out fetching a new connection from the connection pool.

After (with connection pooling):

# Using Supabase pooler URL
DATABASE_URL="postgresql://...@db.xxx.supabase.co:6543/postgres?pgbouncer=true&connection_limit=1"

Database connections stable at 10-15 even under heavy load.

Notes

  • Different managed databases have different pooling solutions—check your provider's docs
  • PlanetScale (MySQL) uses a different architecture and doesn't have this issue
  • connection_limit=1 is aggressive; start there and increase if you see latency
  • The singleton pattern only helps in development; in production serverless, each instance is isolated
  • If using Prisma with Next.js API routes, each route invocation may be a separate serverless function
  • Consider Prisma Accelerate for built-in caching + pooling: https://www.prisma.io/accelerate

Expand your agent's capabilities with these related and highly-rated skills.

blader/Claudeception

continuous-learning

Continuous learning system that extracts reusable knowledge from work sessions. Triggers: (1) /continuous-learning command to review session learnings, (2) "save this as a skill" or "extract a skill from this", (3) "what did we learn?", (4) After any task involving non-obvious debugging, workarounds, or trial-and-error discovery. Creates new Claude Code skills when valuable, reusable knowledge is identified.

2,196 14
Explore
blader/Claudeception

typescript-circular-dependency

Detect and resolve TypeScript/JavaScript circular import dependencies. Use when: (1) "Cannot access 'X' before initialization" at runtime, (2) Import returns undefined unexpectedly, (3) "ReferenceError: Cannot access X before initialization", (4) Type errors that disappear when you change import order, (5) Jest/Vitest tests fail with undefined imports that work in browser.

2,196 14
Explore
blader/Claudeception

nextjs-server-side-error-debugging

Debug getServerSideProps and getStaticProps errors in Next.js. Use when: (1) Page shows generic error but browser console is empty, (2) API routes return 500 with no details, (3) Server-side code fails silently, (4) Error only occurs on refresh not client navigation. Check terminal/server logs instead of browser for actual error messages.

2,196 14
Explore
mattpocock/skills

handoff

Compact the current conversation into a handoff document for another agent to pick up.

111,310 9,758
Explore
mattpocock/skills

setup-pre-commit

Set up Husky pre-commit hooks with lint-staged (Prettier), type checking, and tests in the current repo. Use when user wants to add pre-commit hooks, set up Husky, configure lint-staged, or add commit-time formatting/typechecking/testing.

111,310 9,758
Explore
mattpocock/skills

obsidian-vault

Search, create, and manage notes in the Obsidian vault with wikilinks and index notes. Use when user wants to find, create, or organize notes in Obsidian.

111,310 9,758
Explore

Didn't find tool you were looking for?

Be as detailed as possible for better results