What's the problem?
- I'm using node-mysql to connect to mysql.
- I have a really hard time dealing with the
server disconnects/wait_timeoutsas mentioned here: https://github.com/felixge/node-mysql#server-disconnects - I receive the error message:
This socket has been ended by the other partyevery time after I'm trying to recreate the connection upon handling aPROTOCOL_CONNECTION_LOSTerror.
What I'm trying to do
// Connect Function
db.connect = function(callback){
// Destory the Connection if there is already one
if(db.connection) {
console.log('[mysql]','connection destroy');
db.connection.destroy();
db.connection = null;
}
// Create the Connection
db.connection = MySQL.createConnection(options);
// Connect using the Connection
db.connection.connect(function(error) {
if(error){
console.log('[mysql]', 'connect failed', error);
} else {
console.log('[mysql]', 'connection success');
db.connection.database = options.database;
callback(db.connection, error);
}
});
// Listen on Connection Errors
db.connection.on('error', function(error) {
// Connection to the MySQL server is usually
// lost due to either server restart, or a
// connnection idle timeout (the wait_timeout server variable configures this)
if(error.code === 'PROTOCOL_CONNECTION_LOST') {
console.log('[mysql]', 'PROTOCOL_CONNECTION_LOST')
db.connect(callback);
} else {
console.log('[mysql] Connection Error: ', error);
}
});
// Return Connection Instance
return db.connection;
}
Additional Details
wait_timeout and interactive_timeout has to be set around 10 seconds in my.cnf to test the issue.
[mysqld]
wait_timeout = 10
interactive_timeout = 10