I am trying to setup a connection to my MySQL database and so far it is working, but when i run the nodejs application, and i don't read/update anything in my database the connection shuts down with:
Error: Connection lost The server closed the connection
So i searched on google and found this stackoverflow question. But i cannot get it to work, and i think it is because i am working with a class and not a function.
Below is the code i currently have:
class ConnectionFactory {
constructor() {
this.connectAndHandleDisconnect();
}
connectAndHandleDisconnect() {
connection = mysql.createConnection(db_config);
connection.connect(function(err) {
if(err) {
console.log('error when connecting to db:', err);
setTimeout(connectAndHandleDisconnect, 2000);
}
});
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
connectAndHandleDisconnect(); // Error pops up from this line
} else {
throw err;
}
});
}
}
And i am getting the following error message:
ReferenceError: connectAndHandleDisconnect is not defined
I have tried to replace the connectAndHandleDisconnect() to this.connectAndHandleDisconnect() and self.connectAndHandleDisconnect() but i have no clue how to find the fix to this error.