mysql createPool
This commit is contained in:
@@ -1,16 +1,9 @@
|
||||
// pages/api/addLocation.js
|
||||
import mysql from "mysql";
|
||||
import getPool from "../../../../utils/mysqlPool"; // Singleton-Pool importieren
|
||||
|
||||
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,
|
||||
connectionLimit: 10, // Maximale Anzahl gleichzeitiger Verbindungen
|
||||
});
|
||||
export default async function handler(req, res) {
|
||||
const pool = getPool(); // Singleton-Pool verwenden
|
||||
|
||||
export default function handler(req, res) {
|
||||
if (req.method === "POST") {
|
||||
const { name, poiTypeId, latitude, longitude, idLD } = req.body;
|
||||
console.log("Received data:", req.body); // Überprüfen der empfangenen Daten
|
||||
@@ -19,17 +12,24 @@ export default function handler(req, res) {
|
||||
const point = `POINT(${longitude} ${latitude})`;
|
||||
const values = [name, poiTypeId, point, idLD];
|
||||
|
||||
// Verwende den Pool, um eine Verbindung zu bekommen und die Query auszuführen
|
||||
pool.query(query, values, (error, results) => {
|
||||
if (error) {
|
||||
console.error("Fehler beim Einfügen des Standorts:", error);
|
||||
return res.status(500).json({ error: "Ein Fehler ist aufgetreten" });
|
||||
}
|
||||
let connection;
|
||||
|
||||
try {
|
||||
connection = await pool.getConnection(); // Hole eine Verbindung aus dem Pool
|
||||
|
||||
// Verwende die Verbindung, um die Query auszuführen
|
||||
const [results] = await connection.query(query, values);
|
||||
|
||||
res.status(200).json({
|
||||
id: results.insertId,
|
||||
message: "Standort erfolgreich hinzugefügt",
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Fehler beim Einfügen des Standorts:", error);
|
||||
res.status(500).json({ error: "Ein Fehler ist aufgetreten" });
|
||||
} finally {
|
||||
if (connection) connection.release(); // Gib die Verbindung in den Pool zurück
|
||||
}
|
||||
} else {
|
||||
res.setHeader("Allow", ["POST"]);
|
||||
res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
|
||||
Reference in New Issue
Block a user