I run an web server written in NodeJS/ EXPRESS that deals with a MySQL db. In order to be more efficient, I use the pooling mechanism of node-mysql.
An I still get some issues about : - connection lost - cannot release already release connection - there is no release of undefined
Depending on the request, various code pattern can then be used to get the data and release the connection :
1/ standard callback ...,function(err, rows){...}
- I may understood, that the release shall be done before the callback ? Please confirm me !
- May I even use the release at the first line of the call back function ? (because I may have many case in the case with various callback.
2/ with event
- I put a connection.release in both events .on('error') and .on('end'). Is that enough ?
- What happen if a error is called on they 10th rows. Does both event will be called ? I think not, but I got some issue (can,not release a already release connection) with the following code :
pool.getConnection(function(err, mysqlConnection) {
if (err != null) {
console.log(err);
}
else {
mysqlConnection.query('USE table');
var xs = new Array();
if (selectedB === 'B') {
if (selectedC === 'C') {
var queryCmd = "SELECT DISTINCT nom FROM x WHERE a ='" + selectedA + "'";
var query = mysqlConnection.query(queryCmd);
query
// ERROR
.on('error', function(err) {
mysqlConnection.release();
})
// RESULT
.on('result', function(row) {
mysqlConnection.pause();
var x = row['nom'];
xs.push(x);
mysqlConnection.resume();
})
// END
.on('end', function() {
mysqlConnection.release();
callback(xs);
})
}
else {
mysqlConnection.release();
callback([]);
}
}
else {
mysqlConnection.release();
callback([]);
}
}
});
3/ with return - call release before return !
Then, there are some snippets about error handling for standard connection, what should we do for connection pooling ? cause in case, it crash the server !
Thanks in advance for you advises. That will definitively help me a lot !