I have a nodejs app that connects to mysql When I leave connection open the server close it and when I use con.destroy or con.end I cant reconnect to mysql.
What should I do and is it better to keep closing and opining new connection for each request knowing there will be a lot.
module.exports = class DataAccessLayer {
constructor() {
this.con = mysql.createConnection({
host: ("host.com"),
user: ("asdfg"),
password: ("zxcvb"),
database: ("DB_example")
});
this.con.connect(function (err) {
if (err) throw err;
console.log("Connected to DB");
});
}
getUserBySenderId(senderId) {
this.con.query("SELECT sender_id,first_name,last_name,creation_date " +
"FROM USER " +
"WHERE sender_id = '" + senderId + "'",
function (err, result, fields) {
if (err) throw err;
console.log(result);
});
}