mysql createPool
This commit is contained in:
@@ -1,19 +1,9 @@
|
||||
// API in /api/talas_v5_DB/locationDevice/locationDeviceNameById.js
|
||||
import mysql from "mysql2/promise";
|
||||
|
||||
// Erstellen eines Pools von Datenbankverbindungen
|
||||
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"; // Singleton-Pool importieren
|
||||
|
||||
export default async function handler(req, res) {
|
||||
const pool = getPool(); // Singleton-Pool verwenden
|
||||
|
||||
if (req.method !== "GET") {
|
||||
return res.status(405).json({ error: "Nur GET Methode erlaubt" });
|
||||
}
|
||||
@@ -24,9 +14,13 @@ export default async function handler(req, res) {
|
||||
return res.status(400).json({ error: "idLD ist erforderlich" });
|
||||
}
|
||||
|
||||
let connection;
|
||||
|
||||
try {
|
||||
connection = await pool.getConnection(); // Hole eine Verbindung aus dem Pool
|
||||
|
||||
const query = "SELECT name FROM location_device WHERE idLD = ?";
|
||||
const [results] = await pool.query(query, [idLD]);
|
||||
const [results] = await connection.query(query, [idLD]);
|
||||
|
||||
if (results.length > 0) {
|
||||
res.status(200).json({ name: results[0].name });
|
||||
@@ -36,5 +30,7 @@ export default async function handler(req, res) {
|
||||
} catch (error) {
|
||||
console.error("Fehler beim Abrufen des Gerätenamens:", error);
|
||||
res.status(500).json({ error: "Fehler beim Abrufen des Gerätenamens" });
|
||||
} finally {
|
||||
if (connection) connection.release(); // Gib die Verbindung in den Pool zurück
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user