mysql icon indicating copy to clipboard operation
mysql copied to clipboard

Pool Connection Limit Doesn't take effect!? HELP!

Open lokielse opened this issue 13 years ago • 16 comments

//when slow request the pool work well. //when frequent request, seems that Pool Connection Limit Doesn't take effect! //SHOW STATUS LIKE '%threads_connected%'; the connections count increase slowly, about +1/s //connectionLimit config should limit the connection count! //so what's the problem? how to solve it.

//create pool

opts = utils.extend({
  host     : DB.DB_HOST,
  user     : DB.DB_USER,
  password : DB.DB_PASSWD,
  database : DB.DB_NAME,
  connectionLimit : 30
}, opts);   
this.pool  = mysql.createPool(opts);

//DB@query method

DB.prototype.query = function(sql, param, cbk) {
 var _this;

 _this = this;

 this.pool.getConnection(function(err, client) {
 if (err) {
    client.release();
     } else {
     client.query(sql, param, function(err, rows, fields) {
      client.release();
      if (cbk != null) {
        try{
            cbk(err, rows, fields);
        }catch(e){
            console.log(new Date().toLocaleDateString(), 'sql exception:', e);
        }
      }
    });
  }
});
};

lokielse avatar Aug 21 '13 22:08 lokielse

I suspect that after your utils.extend call, opts.connectionLimit is probably undefined instead of a number, which would cause there to be no connection limit. Please test your code again without that to check (and perhaps log out pool.config.connectionLimit to check if it is a number).

dougwilson avatar Aug 21 '13 22:08 dougwilson

pool.config.connectionLimit = 30;

lokielse avatar Aug 22 '13 02:08 lokielse

P.S.: _allConnections.length is no more than pool.config.connectionLimit

lokielse avatar Aug 22 '13 03:08 lokielse

I just started running into a similar problem. We've got our connection pool configured to use 40 connections, maximum, but every getConnection appears to create an new connection. We're aiming to run 1000 transactions in 60 seconds, and after about 15 seconds the database starts responding that we have "Too many connections" (ER_CON_COUNT_ERROR), and our database config specifies that we allow approximately 250 connections. Easy math says that for some reason every getConnection creates a new connection instead of using an old one. Not sure what's going on here.

skeggse avatar Aug 23 '13 19:08 skeggse

I had the similiar problem. Every time new query created new connection and the connection didn't closed after execution query :(

The code was like this

var mysql = require('mysql');
var pool  = mysql.createPool(...);
pool.getConnection(function(err, connection) {
  // Use the connection
  connection.query( 'SELECT something FROM sometable', function(err, rows) {
    // And done with the connection.
    connection.end();
    // Don't use the connection here, it has been returned to the pool.
  });
});

After I read this https://github.com/felixge/node-mysql#streaming-query-rows I found the solution of the problem.

var mysql = require('mysql');
pool.getConnection(function (err, connection) {
.........
}
var execute = function (sql, params, callback) {
    pool.getConnection(function (err, connection) {
        if (err) {
            logger.error('Mysql connection error: ' + err);
            callback(err, true);
            return;
        }
        var query = connection.query(sql, params,callback);
        query.on('error', function(err) {
            logger.error('Mysql query error: ' + err);
            callback(err, true);
        });
        query.on('result', function(rows) {
            callback(false, rows);
        });
        query.on('end', function() {
            connection.release();
        });
    });
};

This code works seamlessly with the closing of the MySQL connection...

seelnolu avatar Aug 28 '13 10:08 seelnolu

I was actually having problems with the pool limiting my connections because I hadn't closed them correctly, so just to clarify, you run your code and when you have high request volumes your database is reporting over 30 database connections but pool is still creating new connections and says it has less than 30 connections?

Is this the only thing connecting to the database or are you running more than one instance of your code at the same time? Have you restarted your database and re-run the code?

A couple of things confuse me about this code.

  1. Why are you assigning this to _this?
  2. Why are your calling client.release() on a non object when there is an error? If there is an error there is no connection object. Would be nicer if it was simple if(err) { return; }
  3. Why do you have a try catch block around your callback? Surely the callback is for data processing and would not be throwing SQL related errors as your log message suggests?
  4. I was under the impression that the query callback only returns two arguments the first being error and the second being the data. Why do you have three err, rows and fields?

ghost avatar Aug 28 '13 12:08 ghost

@apparatusdeus hey!~

  1. _this is not being used here, just ignore it.
  2. if there is an error, the client will be a non object? sure?
  3. no catch before, just for debuging...
  4. only two arguments? sure?

lokielse avatar Aug 28 '13 13:08 lokielse

  1. Yes I'm sure.
  2. From all the examples and from my own experience. Yes there should only be two arguments.

ghost avatar Aug 28 '13 14:08 ghost

@waisir @apparatusdeus there is indeed a third fields argument provided to the callback.

dougwilson avatar Aug 28 '13 14:08 dougwilson

yeah!~ the doc always tell a truth @apparatusdeus: https://github.com/felixge/node-mysql#introduction

lokielse avatar Aug 28 '13 14:08 lokielse

@waisir @dougwilson I stand corrected. Now I'm going to go and work out what its for :P

ghost avatar Aug 28 '13 14:08 ghost

Similar to @apparatusdeus, it turned out that in a fringe case where I just passed a noop for a callback, the query caused a mysql error. This invalidated the connection, but because I didn't check for an error in said callback, it was never released.

skeggse avatar Aug 30 '13 01:08 skeggse

Feel free to reopen, if your problem still exists.

kai-koch avatar Oct 27 '13 22:10 kai-koch

I now ended up with this same issue. Connection pooling keeps incrementing for every select statement. Eventually, ends up in ER_CON_COUNT_ERROR: Too many connections.

I have connection limit of 5 which doesn't take effect.

Does anyone solved this issue?

Please help me.

Many thanks, Karthik

mithralaya avatar Mar 27 '14 17:03 mithralaya

Hi @mithralaya , can you open a new issue for this so we don't flood all the other people in this issue? Also, please provide some code that I can use to reproduce, because if I specify connectionLimit to be 5, I'm not seeing the issue with the pool.

dougwilson avatar Mar 27 '14 17:03 dougwilson

thank you @dougwilson I will now.

mithralaya avatar Mar 27 '14 17:03 mithralaya