J
J
jtag2017-10-20 13:11:20
MySQL
jtag, 2017-10-20 13:11:20

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);

The function works as it should.
However, when queries are started in the database ( query("SELECT ..." ), then when the database is disabled, the if(err) throw err; error handler is triggered and the server crashes.
What are the standard means to avoid server crashes?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robot Chappie, 2017-10-20
@msa6886

design:

try { 
... 
} catch(err) {
...
}

thus the server will not fall more details here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question