Run a number of queries with a single connection from a pool
suggest changeA pool can run queries one connection at a time. Two statements such as SELECT 1; SELECT 2; still need two query calls in the mysql driver. pool.query checks a connection out and releases it for each call, so the second query waits if the pool has no free connection.
pool.getConnection keeps one connection until you release it:
pool.getConnection(function (err, conn) {
if (err) return callback(err);
conn.query('SELECT 1 AS seq', function (err, rows) {
if (err) {
conn.release();
return callback(err);
}
conn.query('SELECT 2 AS seq', function (err, rows) {
conn.release();
if (err) return callback(err);
callback();
});
});
});
Release the connection on every path, including errors. Otherwise the pool is one connection smaller for the rest of the process.
The mysql package documents this under pooling connections.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents