var mysql = require('mysql'); const connectionPool = mysql.createPool(process.env.DATABASE_CREDENTIALS);
export default async function executeQuery({ query, values }) {
return new Promise((resolve, reject) => {
connectionPool.getConnection((err, connection) => {
if (err) {
// Release the connection back to the pool (if obtained)
if (connection) connection.destroy();
return reject(err);
} else {
connection.query(query, values, (err, results) => {
// Release the connection back to the pool
connection.destroy();
if (err) {
return reject(err);
} else {
// console.log(results);
resolve(results);
}
});
}
});
});
}
I execute my queries through this and I observed that after every query the process goes in sleep and not releases. One of the processes is like this:
Id, User, Host, db, Command, Time, State, Info
189, root, localhost:49214, erp_software_db, Sleep, 1334, ,
After I close the connection manually, all the processes are killed but on restarting the id continues from where it was left and not 1.
But after certain time many processes are just accumulated and I get an error saying "Too many connections".
Can anyone guide me through this?? Thanks for your replies in advance.
Just now I tried using connection.destroy() instead of connection.release(), and this seems to help but I am not sure since for first few attempts it worked and for a moment it didn't. So kindly do explain this too as is it a good choice to use or not ?