Last active
August 29, 2015 14:22
-
-
Save ianldgs/d37e5d3dc7d86db2b7a4 to your computer and use it in GitHub Desktop.
Keep mysql connection alive
This file contains 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
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