mysql createPool
This commit is contained in:
@@ -1,32 +1,27 @@
|
||||
// pages/api/talas_v5_DB/pois/readLocations.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
|
||||
|
||||
const query = `
|
||||
SELECT idPoi, description, idPoiTyp, idLD, ST_AsText(position) AS position
|
||||
FROM poi
|
||||
`;
|
||||
|
||||
let connection;
|
||||
|
||||
try {
|
||||
// Ausführen der Abfrage mit dem Verbindungspool
|
||||
const [results] = await pool.query(query);
|
||||
connection = await pool.getConnection(); // Hole eine Verbindung aus dem Pool
|
||||
|
||||
const [results] = await connection.query(query);
|
||||
|
||||
// Senden der Antwort zurück
|
||||
res.status(200).json(results);
|
||||
} catch (error) {
|
||||
console.error("Fehler beim Abrufen der API:", error);
|
||||
res.status(500).json({ error: "Fehler bei der Abfrage" });
|
||||
} finally {
|
||||
if (connection) connection.release(); // Gib die Verbindung in den Pool zurück
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user