Pool Connection Limit Doesn't take effect!? HELP!
//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);
}
}
});
}
});
};
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).
pool.config.connectionLimit = 30;
P.S.: _allConnections.length is no more than pool.config.connectionLimit
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.
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...
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.
- Why are you assigning this to _this?
- 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; }
- 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?
- 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?
@apparatusdeus hey!~
- _this is not being used here, just ignore it.
- if there is an error, the client will be a non object? sure?
- no catch before, just for debuging...
- only two arguments? sure?
- Yes I'm sure.
- From all the examples and from my own experience. Yes there should only be two arguments.
@waisir @apparatusdeus there is indeed a third fields argument provided to the callback.
yeah!~ the doc always tell a truth @apparatusdeus: https://github.com/felixge/node-mysql#introduction
@waisir @dougwilson I stand corrected. Now I'm going to go and work out what its for :P
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.
Feel free to reopen, if your problem still exists.
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
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.
thank you @dougwilson I will now.