fix: kein DB Verbindung von der Anwendung
Der Fehler war, dass im Code die Funktion getDebugLog() verwendet wurde, die nicht definiert war. Dadurch ist beim Erstellen des Datenbank-Pools ein Fehler aufgetreten, bevor überhaupt eine Verbindung zur Datenbank aufgebaut werden konnte. Erst nachdem die Debug-Logik entfernt wurde, konnte die Verbindung erfolgreich hergestellt werden. Das Problem lag also nicht an der Datenbank oder an den Zugangsdaten, sondern an einem fehlenden bzw. nicht importierten Hilfsfunktion im Pool-Code.
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
import mysql from "mysql2/promise";
|
||||
// Variablen für den Singleton-Pool
|
||||
let cachedPool;
|
||||
let connectionCount = 0; // Zähler für die aktiven Verbindungen
|
||||
let connectionCount = 0;
|
||||
|
||||
// Funktion zum Abrufen des Pools
|
||||
function getPool() {
|
||||
if (!cachedPool) {
|
||||
cachedPool = mysql.createPool({
|
||||
@@ -12,12 +10,11 @@ function getPool() {
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.DB_NAME,
|
||||
port: process.env.DB_PORT,
|
||||
connectionLimit: 20, // Setze ein Limit für gleichzeitige Verbindungen
|
||||
connectionLimit: 20,
|
||||
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
|
||||
queueLimit: 10,
|
||||
connectTimeout: 5000,
|
||||
idleTimeout: 60000,
|
||||
});
|
||||
|
||||
// Ereignisse für das Protokollieren der Verbindungsstatistiken
|
||||
@@ -25,46 +22,27 @@ function getPool() {
|
||||
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
// Debug-Logging entfernt
|
||||
if (connectionCount > maxUsed) {
|
||||
maxUsed = connectionCount;
|
||||
}
|
||||
});
|
||||
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");
|
||||
}
|
||||
}
|
||||
// Debug-Logging entfernt
|
||||
});
|
||||
}
|
||||
|
||||
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.
|
||||
cachedPool = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user