Repeatedly getting CONNECT_TIMEOUT and MaxClientsInSessionMode during local development.

Hello. I am using Nextjs 16. During local development, when I call database from drizzle, I get a CONNECT_TIMEOUT or sometimes MaxClientsInSessionMode if I trigger Nextjs hot reload quickly. My initial guess was that I was probably creating a lot of connections during hot module reloads. Also I switched to transactional connect rather than session pooler based on this https://github.com/orgs/supabase/discussions/22305. So I implemented this Singleton pattern, which still doesn't work -

import { drizzle, type PostgresJsDatabase } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "@/schemas/schema";

declare global {
  var database: PostgresJsDatabase<typeof schema> | undefined;
}

// Disable prefetch as it is not supported for "Transaction" pool mode
const client = postgres(process.env.DATABASE_URL as string, { prepare: false });
const instance = drizzle(client, { schema });

if (process.env.NODE_ENV !== "production") {
  global.database = instance;
}

export function getDB(): PostgresJsDatabase<typeof schema> {
  return global.database || instance;
}


I am still getting this issue after every 5/6 consecutive calls to the database during Hot Module Refresh. What to do? Please help.
error.txt7.16KB
Was this page helpful?