Answer the question
In order to leave comments, you need to log in
How to reconnect when the database is disconnected?
I'm using the 'mysql' module for node.js . The server connects to mysql remotely. When the database is turned off, the server constantly crashes and you have to turn it on manually via "systemctl" (for some reason, systemctl makes about 10 turns on after 1 second and stops). I found a code on github that does reconnect until the connection is restored.
function handleDisconnect(myconnection) {
myconnection.on('error', function (err) {
console.log('\nRe-connecting lost connection: ' + new Date());
con.destroy();
con = mysql.createConnection(options);
handleDisconnect(con);
con.connect();
});
}
handleDisconnect(con);
Answer the question
In order to leave comments, you need to log in
design:
try {
...
} catch(err) {
...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question