Mysql: Using pool.query directly will release the connection automatically?

Created on 31 Aug 2015  路  7Comments  路  Source: mysqljs/mysql

From the sample code

var mysql = require('mysql');
var pool  = mysql.createPool({
  connectionLimit : 10,
  host            : 'example.org',
  user            : 'bob',
  password        : 'secret'
});

pool.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;

  console.log('The solution is: ', rows[0].solution);
});

Is the pool will be released automatically if I use above codes? Just wondering why my query stuck after running several times of queries

question

Most helpful comment

yes. pool.query() is shortcut for pool.getConnection() + connection.query() + connection.release() - see https://github.com/felixge/node-mysql/blob/master/lib/Pool.js#L194-L207

All 7 comments

yes. pool.query() is shortcut for pool.getConnection() + connection.query() + connection.release() - see https://github.com/felixge/node-mysql/blob/master/lib/Pool.js#L194-L207

thanks!
But I wonder what is the main cause of my query callback never returned.
It happened after several queries using pool.query(). There is no error at all
Anyone had similar issue?

Is that your entire program? It's hard to really answer without the entire program to look at/debug.

pool.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;
  console.log('causing uncaught exception: ', object.empty.null);
});

I guess uncaught exception inside query callback is the problem. When it happened multiple times, mysql connection will hang up

An uncaught exception should be crashing your entire Node.js app, so you shouldn't be doing any further queries after one anyway.

The note from the Node.js documentation on https://nodejs.org/api/process.html#process_event_uncaughtexception

Note that uncaughtException is a very crude mechanism for exception handling.

Don't use it, use domains instead. If you do use it, restart your application after every unhandled exception!

Do not use it as the node.js equivalent of On Error Resume Next. An unhandled exception means your application - and by extension node.js itself - is in an undefined state. Blindly resuming means anything could happen.

Think of resuming as pulling the power cord when you are upgrading your system. Nine out of ten times nothing happens - but the 10th time, your system is bust.

You have been warned.

Thanks Doug!!

@sidorares , during workload test I discovered I reach out db connection limit when exploit connections pool without releasing connection

        pool.getConnection(function(err, connection) {
            connection.query(
                {sql: 'SELECT * FROM course', timeout: 60000}, 
                function(error, rows, fields) {
                    if (error != null) {
                        resolve(JSON.stringify(util.getErrorMessage()))
                    } else {
                        resolve(JSON.stringify(util.getPayloadMessage(rows)))
                    }
            })
        });

Adding connection.release() solves the issue.

A quick reading over docs and comments blurred the difference between pool.query() and pool.getConnection(). The second case doesn't have automatic connection management and you still have to release it manually. I would leave this here for similar cases.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abou7mied picture abou7mied  路  4Comments

PeppeL-G picture PeppeL-G  路  3Comments

bologer picture bologer  路  3Comments

johnrc picture johnrc  路  3Comments

ajpyoung picture ajpyoung  路  4Comments