N
Neon7mo ago
plain-purple

Uncaught Exception: Error: Connection terminated unexpectedly

I have a Next.js app deployed on Vercel, using @neondatabase/serverless as a PostgreSQL client and @prisma/adapter-neon to integrate with Prisma. Occasionally, I get a Connection terminated unexpectedly error, which causes an unhandled exception and crashes the Node.js process with exit code 129 (SIGHUP). Here’s my setup:
import { Pool, neonConfig } from "@neondatabase/serverless";
import { PrismaNeon } from "@prisma/adapter-neon";
import { PrismaClient } from "@prisma/client";

const connectionString = process.env.DATABASE_URL || "";

// Local/test environment configuration
const isDevelopment = process.env.NODE_ENV === "development";
const isTest = process.env.NODE_ENV === "test";

if (isDevelopment || isTest) {
const LOCAL_HOST = "db.localtest.me";

const getLocalPort = () => (isTest ? 4445 : 4444);

// Override fetchEndpoint
neonConfig.fetchEndpoint = (host) => {
if (host === LOCAL_HOST) {
return `http://${host}:${getLocalPort()}/sql`;
}
return `https://${host}:${443}/sql`;
};

// Secure WebSocket condition
const connectionStringUrl = new URL(connectionString);
neonConfig.useSecureWebSocket = connectionStringUrl.hostname !== LOCAL_HOST;

// wsProxy setting
neonConfig.wsProxy = (host) => {
if (host === LOCAL_HOST) {
return `${host}:${getLocalPort()}/v2`;
}
return `${host}/v2`;
};
}

const pool = new Pool({ connectionString });
const adapter = new PrismaNeon(pool);
const prisma = new PrismaClient({ adapter });

export { prisma };
import { Pool, neonConfig } from "@neondatabase/serverless";
import { PrismaNeon } from "@prisma/adapter-neon";
import { PrismaClient } from "@prisma/client";

const connectionString = process.env.DATABASE_URL || "";

// Local/test environment configuration
const isDevelopment = process.env.NODE_ENV === "development";
const isTest = process.env.NODE_ENV === "test";

if (isDevelopment || isTest) {
const LOCAL_HOST = "db.localtest.me";

const getLocalPort = () => (isTest ? 4445 : 4444);

// Override fetchEndpoint
neonConfig.fetchEndpoint = (host) => {
if (host === LOCAL_HOST) {
return `http://${host}:${getLocalPort()}/sql`;
}
return `https://${host}:${443}/sql`;
};

// Secure WebSocket condition
const connectionStringUrl = new URL(connectionString);
neonConfig.useSecureWebSocket = connectionStringUrl.hostname !== LOCAL_HOST;

// wsProxy setting
neonConfig.wsProxy = (host) => {
if (host === LOCAL_HOST) {
return `${host}:${getLocalPort()}/v2`;
}
return `${host}/v2`;
};
}

const pool = new Pool({ connectionString });
const adapter = new PrismaNeon(pool);
const prisma = new PrismaClient({ adapter });

export { prisma };
Despite using the @neondatabase/serverless pool, which should be suited for serverless environments, I still see intermittent disconnections reported by Vercel logs. Does anyone know what might cause this in a serverless environment, and how to prevent these unexpected terminations?
6 Replies
plain-purple
plain-purpleOP7mo ago
Bump
wise-white
wise-white7mo ago
I looked at similar errors in our Support system. What I'm seeing is: - Make sure you're running the latest Prisma Client and Neon Sererless driver - Look at connection pool timeout settings: https://neon.tech/docs/guides/prisma#connection-pool-timeouts @Mahmoud or @Rishi Raj Jain anything you might suggest here?
Neon
Connect from Prisma to Neon - Neon Docs
Prisma is an open source, next generation ORM that lets you to manage and interact with your database. This guide covers the following topics Connect to Neon from Prisma Use connection pooling with Pr...
plain-purple
plain-purpleOP7mo ago
@Daniel Thank you! Updated Prisma Client and Neon Serverless driver to their latest versions. Checked the connection pool timeout settings as outlined in the Neon documentation. However, the error still persists. Additionally, as shown in the attached image, it appears that timeout settings cannot be changed in the Vercel environment. Could you please advise on any further steps or workarounds to resolve this issue?
No description
wise-white
wise-white7mo ago
I see that you've been able to open a Support ticket. That's great. Hopefully, they can get you sorted out.
other-emerald
other-emerald7mo ago
@tomolld Check out the example at https://github.com/neondatabase/examples/blob/main/with-nextjs-prisma-edge/lib/prisma.server.ts (and match the code of instantiating the Prisma instance) which should work with Prisma, Next.js, Neon and Vercel and get rid of the sporadic errors.
plain-purple
plain-purpleOP7mo ago
@Rishi Raj Jain This is works! Thank you!

Did you find this page helpful?