Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I do not see a graceful disconnect in the express example #7263

Closed
jared-dahlke-dexcom opened this issue Dec 18, 2024 · 1 comment
Closed

Comments

@jared-dahlke-dexcom
Copy link

I'm doing this in my express app to gracefully disconnect from postgres. Is this not better? I don't see anything like this in your express example unless I'm missing it:

...other code

import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();

const gracefulPrismaPostgresShutdown = async () => {
    console.log("Shutting down prisma postgres connection gracefully...");
    await prisma.$disconnect();
    process.exit(0);
};
// Handle termination signals
process.on("SIGINT", gracefulPrismaPostgresShutdown);
process.on("SIGTERM", gracefulPrismaPostgresShutdown);

app.use(async (err, req, res, next) => {
    if (err) {
        console.error("Unhandled error:", err);
        await prisma.$disconnect(); // Ensure disconnect
        respondError(res, err ?? "Technical Error");
    }
    next();
});

// Set up global middleware
app.use(cors(), express.json(), authenticateJWT);

/**
 * ROUTES
 */

// Get all subscriptions
app.get("/subscriptions", (req, res, next) => {
    subscriptionsGet(req, res, next);
});


@jharrell
Copy link
Member

Hey there! In most cases, this isn't required. $connect and $disconnect are called internally by the Client when required. Here's some of our docs that describe the inner workings: https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/databases-connections/connection-management

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants