Skip to content

Instantly share code, notes, and snippets.

@ianldgs
Last active August 29, 2015 14:22
Show Gist options
  • Save ianldgs/d37e5d3dc7d86db2b7a4 to your computer and use it in GitHub Desktop.
Save ianldgs/d37e5d3dc7d86db2b7a4 to your computer and use it in GitHub Desktop.
Keep mysql connection alive
var con
var keepAlive = function () {
console.log('Trying MySQL connection')
con = mysql.createConnection({host: 'localhost', user: 'root', password: '', database: 'default'})
con.connect(function (err) {
if (err) return console.log('MySQL connection failed', err.stack)
console.log('Connected to MySQL')
})
con.on('error', function (err) {
console.log('MySQL error', err.stack)
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
console.log('It\'s a connection error, reconnect')
keepAlive()
}
})
}; keepAlive()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment