Skip to content

Instantly share code, notes, and snippets.

@tralamazza
Created July 27, 2012 14:49
Show Gist options
  • Select an option

  • Save tralamazza/3188464 to your computer and use it in GitHub Desktop.

Select an option

Save tralamazza/3188464 to your computer and use it in GitHub Desktop.
node mysql server disconnect
// source: https://github.com/felixge/node-mysql#server-disconnects
var mysql = require('mysql');
function supportReconnect(connection, cb) {
connection.on('error', function(err) {
if (!err.fatal) return;
if (err.code !== 'PROTOCOL_CONNECTION_LOST') throw err;
connection = mysql.createConnection(connection.config);
if (cb(connection))
supportReconnect(connection, cb);
connection.connect();
}
}
// ...
var foo = {
bar: mysql.createConnection({ host: 'localhost', user: 'root' })
}
supportReconnect(foo.bar, function(newConnection) {
return foo.bar = newConnection;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment