mysql createPool

This commit is contained in:
ISA
2024-09-02 09:06:49 +02:00
parent ac80c8c619
commit ea46bd771b
52 changed files with 2026 additions and 437 deletions

View File

@@ -1,27 +1,21 @@
// /pages/api/talas_v5_DB/station/getAllStationsNames.js
import mysql from "mysql2/promise";
// Verbindungspool-Konfiguration
const pool = 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,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
});
import getPool from "../../../../utils/mysqlPool"; // Importiere den Singleton-Pool
export default async function handler(req, res) {
const pool = getPool(); // Verwende den Singleton-Pool
if (req.method !== "GET") {
res.setHeader("Allow", ["GET"]);
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
let connection;
try {
connection = await pool.getConnection(); // Hole eine Verbindung aus dem Pool
// Abrufen aller idLD und ihrer Namen
const [results] = await pool.query("SELECT idLD, name FROM location_device");
const [results] = await connection.query("SELECT idLD, name FROM location_device");
if (results.length === 0) {
return res.status(404).json({ error: "No data found" });
@@ -39,5 +33,7 @@ export default async function handler(req, res) {
} catch (err) {
console.error("Fehler beim Abrufen der Daten:", err);
res.status(500).json({ error: "Error retrieving data from the database" });
} finally {
if (connection) connection.release(); // Gib die Verbindung zurück in den Pool
}
}