mysql createPool
This commit is contained in:
@@ -1,20 +1,9 @@
|
||||
// pages/api/talas_v5_DB/pois/updateLocation.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,
|
||||
charset: "utf8mb4",
|
||||
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 !== "POST") {
|
||||
res.setHeader("Allow", ["POST"]);
|
||||
return res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
@@ -28,8 +17,12 @@ export default async function handler(req, res) {
|
||||
|
||||
const query = "UPDATE poi SET position = POINT(?, ?) WHERE idPoi = ?";
|
||||
|
||||
let connection;
|
||||
|
||||
try {
|
||||
const [result] = await pool.query(query, [longitude, latitude, id]);
|
||||
connection = await pool.getConnection(); // Hole eine Verbindung aus dem Pool
|
||||
|
||||
const [result] = await connection.query(query, [longitude, latitude, id]);
|
||||
|
||||
if (result.affectedRows > 0) {
|
||||
res.status(200).json({ success: true });
|
||||
@@ -39,44 +32,7 @@ export default async function handler(req, res) {
|
||||
} catch (error) {
|
||||
console.error("Fehler beim Aktualisieren der Position:", error);
|
||||
res.status(500).json({ error: "Ein Fehler ist aufgetreten" });
|
||||
} finally {
|
||||
if (connection) connection.release(); // Gib die Verbindung in den Pool zurück
|
||||
}
|
||||
}
|
||||
|
||||
/* import mysql from "mysql";
|
||||
import util from "util";
|
||||
|
||||
const dbConfig = {
|
||||
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,
|
||||
charset: "utf8mb4",
|
||||
};
|
||||
|
||||
export default async function handler(req, res) {
|
||||
if (req.method !== "POST") {
|
||||
res.setHeader("Allow", ["POST"]);
|
||||
return res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||
}
|
||||
|
||||
const { id, latitude, longitude } = req.body;
|
||||
|
||||
const connection = mysql.createConnection(dbConfig);
|
||||
// Promisify the query method
|
||||
const query = util.promisify(connection.query).bind(connection);
|
||||
|
||||
try {
|
||||
await query("UPDATE poi SET position = POINT(?, ?) WHERE idPoi = ?", [
|
||||
longitude,
|
||||
latitude,
|
||||
id,
|
||||
]);
|
||||
res.status(200).json({ success: true });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({ error: "Ein Fehler ist aufgetreten" });
|
||||
} finally {
|
||||
connection.end();
|
||||
}
|
||||
} */
|
||||
|
||||
Reference in New Issue
Block a user