-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Closed
Labels
Description
I used below error handling code for connection timeout. Still i face the problem. Did any one faced this problem
var config = require('../config/config');
module.exports = function(){
var mysql = require('mysql');
var db_config = {
host : config.db_host,
user : config.db_username,
password : config.db_password,
database : config.db_name
};
var connection;
function handleDisconnect() {
connection = mysql.createConnection(db_config); // Recreate the connection, since
connection.connect(function(err) {
if(err) {
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000);
}
});
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect();
} else {
throw err;
}
});
}
handleDisconnect();
return connection;
}