Files
nodeMap/utils/mysqlPool.js
ISA 3896381a8f Debug-Logging zentralisiert: Nutzung von process.env.NEXT_PUBLIC_DEBUG_LOG entfernt und auf getDebugLog() mit config.json umgestellt
- Alle Vorkommen von process.env.NEXT_PUBLIC_DEBUG_LOG entfernt
- Debug-Konfiguration erfolgt jetzt ausschließlich über public/config.json
- getDebugLog()-Utility überall verwendet
- .env-Dateien werden für Debug-Logging nicht mehr benötigt
- Alle betroffenen Komponenten, Services und API
2025-08-22 11:10:40 +02:00

72 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import mysql from "mysql2/promise";
// Variablen für den Singleton-Pool
let cachedPool;
let connectionCount = 0; // Zähler für die aktiven Verbindungen
// Funktion zum Abrufen des Pools
function getPool() {
if (!cachedPool) {
cachedPool = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
port: process.env.DB_PORT,
connectionLimit: 20, // Setze ein Limit für gleichzeitige Verbindungen
waitForConnections: true,
queueLimit: 10, // Warteschlangenlimit für Verbindungen
connectTimeout: 5000, // Timeout für Verbindungsversuche (5 Sekunden)
//acquireTimeout: 10000, // Timeout für Verbindungsanforderungen aus dem Pool (10 Sekunden)
idleTimeout: 60000, // 1 Minute
});
// Ereignisse für das Protokollieren der Verbindungsstatistiken
let maxUsed = 0;
cachedPool.on("acquire", () => {
connectionCount++;
if (process.env.NODE_ENV === "development") {
if (getDebugLog()) {
console.log("\x1b[36m%s\x1b[0m", ` Connection acquired (${connectionCount} total)`);
}
if (connectionCount > maxUsed) {
maxUsed = connectionCount;
if (getDebugLog()) {
console.log(`📈 Neue Höchstzahl aktiver gleichzeitiger Verbindungen: ${maxUsed}`);
}
}
}
});
cachedPool.on("release", () => {
connectionCount--;
if (process.env.NODE_ENV === "development") {
if (getDebugLog()) {
console.log("\x1b[32m%s\x1b[0m", ` Connection released (${connectionCount} total)`);
}
}
});
cachedPool.on("enqueue", () => {
if (process.env.NODE_ENV === "development") {
if (getDebugLog()) {
console.warn("\x1b[33m%s\x1b[0m", "⏳ Pool voll Anfrage in Warteschlange");
}
}
});
}
return cachedPool;
}
// Optionale Funktion zum Schließen aller Verbindungen im Pool (manuell aufzurufen)
export function closePool() {
if (cachedPool) {
cachedPool.end(() => {
console.log("All pool connections closed.");
});
cachedPool = null; // Setze den Pool auf null, um sicherzustellen, dass er neu erstellt wird, falls benötigt.
}
}
export default getPool;