Created
July 27, 2012 14:49
-
-
Save tralamazza/3188464 to your computer and use it in GitHub Desktop.
node mysql server disconnect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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