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:
ISA
2025-09-12 12:19:51 +02:00
parent 598acb8441
commit a2d3338624
6 changed files with 33 additions and 37 deletions

View File

@@ -23,4 +23,4 @@ NEXT_PUBLIC_USE_MOCKS=true
# z.B. http://10.10.0.13/xyz/index.aspx -> basePath in config.json auf /xyz setzen # z.B. http://10.10.0.13/xyz/index.aspx -> basePath in config.json auf /xyz setzen
# basePath wird jetzt in public/config.json gepflegt # basePath wird jetzt in public/config.json gepflegt
# App-Versionsnummer # App-Versionsnummer
NEXT_PUBLIC_APP_VERSION=1.1.352 NEXT_PUBLIC_APP_VERSION=1.1.353

View File

@@ -24,4 +24,4 @@ NEXT_PUBLIC_USE_MOCKS=false
# basePath wird jetzt in public/config.json gepflegt # basePath wird jetzt in public/config.json gepflegt
# App-Versionsnummer # App-Versionsnummer
NEXT_PUBLIC_APP_VERSION=1.1.352 NEXT_PUBLIC_APP_VERSION=1.1.353

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "nodemap", "name": "nodemap",
"version": "1.1.352", "version": "1.1.353",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "nodemap", "name": "nodemap",
"version": "1.1.352", "version": "1.1.353",
"dependencies": { "dependencies": {
"@emotion/react": "^11.13.3", "@emotion/react": "^11.13.3",
"@emotion/styled": "^11.13.0", "@emotion/styled": "^11.13.0",

View File

@@ -1,6 +1,6 @@
{ {
"name": "nodemap", "name": "nodemap",
"version": "1.1.352", "version": "1.1.353",
"dependencies": { "dependencies": {
"@emotion/react": "^11.13.3", "@emotion/react": "^11.13.3",
"@emotion/styled": "^11.13.0", "@emotion/styled": "^11.13.0",

View File

@@ -0,0 +1,18 @@
// pages/api/testDbConnection.js
import getPool from "../../utils/mysqlPool";
export default async function handler(req, res) {
const pool = getPool();
let connection;
try {
connection = await pool.getConnection();
const [rows] = await connection.query("SELECT 1 AS test");
console.log("DB-Verbindung erfolgreich! Ergebnis:", rows);
res.status(200).json({ success: true, result: rows });
} catch (error) {
console.error("DB-Verbindungsfehler:", error);
res.status(500).json({ success: false, error: error.message });
} finally {
if (connection) connection.release();
}
}

View File

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