I'm using Azure MySQL database in my Node application which works fine except that after few minutes of usage, Azure closes the connection and Node throws error:
Error: read ECONNRESET
What I need is to prevent my Node app from crashing and tell it to reconnect after such exception. I have tried:
var con = mysql.createConnection({
host: "mydatabase.mysql.database.azure.com",
user: "nodeapp@mydatabase",
password: "12345",
database: "mydatabase",
port: 3306
});
process.on('uncaughtException', function(err) {
con.destroy();
con.connect();
console.log(err);
});
This prevents app from crashing, but my app cannot write to database after this so reconnection fails. How should I reconnect after such error? Any advise is highly appreciated.