update pool

This commit is contained in:
pringgosutono
2026-04-27 13:48:46 +07:00
parent 9dc6fec70e
commit aebdd509e2

View File

@ -43,33 +43,33 @@ pool.getConnection((err, conn) => {
// console.log('Connection %d released', connection.threadId); // console.log('Connection %d released', connection.threadId);
// }); // });
// --- The Deadlock Optimizer --- // // --- The Deadlock Optimizer ---
// This function wraps your queries and retries them if a deadlock occurs // // This function wraps your queries and retries them if a deadlock occurs
const queryWithRetry = async (sql, params, retries = 3) => { // const queryWithRetry = async (sql, params, retries = 3) => {
for (let attempt = 1; attempt <= retries; attempt++) { // for (let attempt = 1; attempt <= retries; attempt++) {
try { // try {
return await pool.execute(sql, params); // return await pool.execute(sql, params);
} catch (err) { // } catch (err) {
const isDeadlock = err.code === 'ER_LOCK_DEADLOCK' || err.errno === 1213; // const isDeadlock = err.code === 'ER_LOCK_DEADLOCK' || err.errno === 1213;
if (isDeadlock && attempt < retries) { // if (isDeadlock && attempt < retries) {
console.warn(`Deadlock detected. Retry attempt ${attempt}...`); // console.warn(`Deadlock detected. Retry attempt ${attempt}...`);
// Wait slightly longer each time (Exponential Backoff) // // Wait slightly longer each time (Exponential Backoff)
await new Promise(res => setTimeout(res, attempt * 100)); // await new Promise(res => setTimeout(res, attempt * 100));
continue; // continue;
} // }
throw err; // If not a deadlock or out of retries, throw // throw err; // If not a deadlock or out of retries, throw
} // }
} // }
}; // };
module.exports = { // module.exports = {
pool, // pool,
query: queryWithRetry // query: queryWithRetry
}; // };
// module.exports = pool; module.exports = pool;