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.
4 Replies
Prisma AI Help
Prisma AI Help5mo ago
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!
Miguel-Adaptavist
Miguel-AdaptavistOP4mo ago
Hi! I have asked this same question in the #ask-ai channel, but I would like to confirm it with the official support. Could be anyone from the support team able to take it? Thanks!
Nurul
Nurul4mo ago
Hey, if your function is running every 30 mins then adding explicit disconnection shouldn't cause any issue. Also, since it's a CRON, it should be okay if a few milliseconds latency is added for prisma to connect to the database, so I don't anticipate any issue.
Miguel-Adaptavist
Miguel-AdaptavistOP4mo ago
Great, thanks a lot!

Did you find this page helpful?