Run a number of queries with a single connection from a pool

suggest change

A 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents