Prisma Client (v.5.10.2) in a Next.js project deployed in Vercel. Use or not the explicit disconnect

Hi! I'm using Prisma (v.5.10.2) in a Next.js project on Vercel. Our endpoint, triggered every 30 minutes by a cron job, interacts with a Neon serverless database. We've seen errors P1017 or P2024 in Vercel logs. The app retries operations up to four times with delays. Vercel support suggested explicitly disconnecting to prevent Prisma errors, referencing this: https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/databases-connections/connection-management#calling-disconnect-explicitly. They recommended:
import { PrismaClient } from '@prisma/client';
import { NextRequest, NextResponse } from 'next/server';

export async function GET(req: NextRequest): Promise<NextResponse> {
const prisma = new PrismaClient(); // use the prisma client…
const result = await prisma.user.findMany();
// disconnect before returning the response
await prisma.$disconnect();
return NextResponse.json(result);
}
import { PrismaClient } from '@prisma/client';
import { NextRequest, NextResponse } from 'next/server';

export async function GET(req: NextRequest): Promise<NextResponse> {
const prisma = new PrismaClient(); // use the prisma client…
const result = await prisma.user.findMany();
// disconnect before returning the response
await prisma.$disconnect();
return NextResponse.json(result);
}
" However, I have some concerns about implementing this. According to this documentation: https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/databases-connections#do-not-explicitly-disconnect-1, explicit disconnection isn't necessary in serverless environments. Additionally, our function isn't something that "runs infrequently" since it executes every 30 minutes. They also suggested the disconnect approach because Vercel uses function bundling (https://vercel.com/docs/functions/configuring-functions/advanced-configuration#bundling-vercel-functions) and mentioned that "Our engineer suggested the disconnect as you can see the reused connections are causing problems [...]" I would like to know if Vercel's support advice is accurate and whether using an explicit disconnect could be a viable option in our scenario. Thanks!
Advanced Configuration
Learn how to add utility files to the /api directory, and bundle Vercel Functions.
Connection management | Prisma Documentation
This page explains how database connections are handled with Prisma Client and how to manually connect and disconnect your database.
1 Reply
Prisma AI Help
You're in no rush, so we'll let a dev step in. Enjoy your coffee, or drop into #ask-ai if you get antsy for a second opinion!

Did you find this page helpful?