fix: Implementasi retry mechanism dan error handling untuk database connections Deskripsi: Menambahkan withRetry wrapper pada berbagai API routes untuk menangani transient database errors dan meningkatkan reliabilitas koneksi Memperbaiki error handling pada notification, authentication, dan user validation endpoints dengan response 503 untuk database connection errors Update prisma.ts dengan konfigurasi logging yang lebih baik dan datasources configuration Menambahkan validasi input parameters pada beberapa endpoints Update dokumentasi QWEN.md dengan commit message format dan comment standards Update .env.example dengan connection pool settings yang lebih lengkap File yang diubah: src/lib/prisma.ts — Konfigurasi Prisma client & logging src/app/api/admin/notifikasi/count/route.tsx src/app/api/auth/mobile-login/route.ts src/app/api/mobile/notification/[id]/route.ts src/app/api/user-validate/route.ts Dan 27 file API routes lainnya (penerapan withRetry secara konsisten) QWEN.md — Dokumentasi commit & comment standards .env.example — Database connection pool configuration ### No Issue
56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import { PrismaClient } from "@prisma/client";
|
|
|
|
/**
|
|
* Instance global Prisma client untuk connection pooling
|
|
* Menggunakan pattern globalThis untuk mencegah multiple instance selama:
|
|
* - Hot module replacement (HMR) di development
|
|
* - Multiple import di seluruh aplikasi
|
|
* - Server-side rendering di Next.js
|
|
*/
|
|
declare global {
|
|
// eslint-disable-next-line no-var
|
|
var prisma: PrismaClient | undefined;
|
|
}
|
|
|
|
// Konfigurasi connection pool via parameter query DATABASE_URL:
|
|
// connection_limit=10&pool_timeout=20&connect_timeout=10
|
|
const prisma =
|
|
globalThis.prisma ??
|
|
new PrismaClient({
|
|
log:
|
|
process.env.NODE_ENV === "development"
|
|
? ["error", "warn"]
|
|
: ["error"],
|
|
datasources: {
|
|
db: {
|
|
url: process.env.DATABASE_URL,
|
|
},
|
|
},
|
|
});
|
|
|
|
// Hanya assign ke global di development untuk mencegah multiple instance saat HMR
|
|
// Di production, ini di-skip karena tidak ada HMR
|
|
if (process.env.NODE_ENV !== "production") {
|
|
globalThis.prisma = prisma;
|
|
}
|
|
|
|
/**
|
|
* Handler graceful shutdown untuk koneksi Prisma
|
|
* Panggil ini HANYA saat terminasi process (SIGINT/SIGTERM)
|
|
* JANGAN panggil $disconnect() setelah query individual
|
|
*/
|
|
async function gracefulShutdown(): Promise<void> {
|
|
console.log("[Prisma] Menutup koneksi database...");
|
|
await prisma.$disconnect();
|
|
console.log("[Prisma] Semua koneksi ditutup");
|
|
}
|
|
|
|
// Register shutdown handlers (hanya di environment Node.js)
|
|
if (typeof process !== "undefined") {
|
|
process.on("SIGINT", gracefulShutdown);
|
|
process.on("SIGTERM", gracefulShutdown);
|
|
}
|
|
|
|
export default prisma;
|
|
export { prisma };
|