Created
July 28, 2014 19:15
-
-
Save imposibrus/056e7db82eb0598e54e6 to your computer and use it in GitHub Desktop.
Node.js MySQL connection pool with node-mysql package
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 mysql = require('mysql'); | |
function handleDisconnect(db_config) { | |
var connection = mysql.createPool(db_config); | |
connection.on('error', function(err) { | |
console.error('db error', err, err.code); | |
if(err.code === 'PROTOCOL_CONNECTION_LOST') { | |
handleDisconnect(db_config); | |
} else { | |
throw err; | |
} | |
}); | |
return connection; | |
} | |
var connection = handleDisconnect({ | |
connectionLimit : 10, | |
host: 'localhost', | |
user: 'root', | |
password: '', | |
database: 'test', | |
dateStrings: true, | |
multipleStatements: true, | |
queryFormat: function (query, values) { | |
if (!values) return query; | |
return query.replace(/\:(\w+)/g, function (txt, key) { | |
if (values.hasOwnProperty(key)) { | |
return this.escape(values[key]); | |
} | |
return txt; | |
}.bind(this)); | |
} | |
}); | |
module.exports = connection; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment