-
Notifications
You must be signed in to change notification settings - Fork 2.5k
node-mysql pool active connections not getting released #999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@abarik1981 there is something missing from the code you posted, because I cannot run it since there is an error when it tries to call |
I have updated at stackoverflow... anyways, i have posted the whole code here... /**
* Test mysql_client (./test.js)
*/
'use strict';
var async = require('async');
var mysqlc = require('../mysql_client');
var connProp = {
connectionLimit : 10,
host : 'xxx',
user : 'xxx',
password : 'xxx',
database : 'xxx'
};
var mydbc = new mysqlc.MySqlClient(connProp);
async.series([
function(callback) {
mydbc.connect(callback);
},
function(callback) {
var intervalObject;
intervalObject = setInterval(function() {
mydbc.runQry('SELECT 1', function(err, results) {
if (err) {
console.error('foo-err:', intervalObject, err);
callback(err);
} else {
console.log('resultsxxxx', results)
}
})
}, 1000*0.5)
},
function(callback) {
mydbc.disconnect(callback);
}
],
function(err, results) {
console.log('results', results[1]);
});
// This results in the following output, finally hitting enqueue event:
connecting mysql
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
resultsxxxx [ { '1': 1 } ]
running mysql qry: SELECT 1
results undefined
runQry-Waiting for available connection slot-ERROR
running mysql qry: SELECT 1
runQry-Waiting for available connection slot-ERROR
running mysql qry: SELECT 1
runQry-Waiting for available connection slot-ERROR
// This is the wrapper for node-mysql (pretty straightforward)
/**
* mysql_client (./mysql_client.js)
*/
// Import modules
var mysql = require('mysql');
var path = require('path');
var fs = require('fs');
var async = require('async');
// Export module with Namespace
var mysqlc = exports;
// Constants
var DEFAULT_MYSQL_PORT = 3306;
var DEFAULT_AUTOCOMMIT = false;
var DEFAULT_CONNLIMIT = 10;
/**
* MySQL Class Wrapper
* @param {string} user: user of database account
* @param {string} password: password for database account
* @param {string} database: database name
* @param {string} host: hostname of server
* @param {string} port: port of server
* @param {boolean} autocommit: autocommit
* @return {object} object
*/
mysqlc.MySqlClient = function MySqlClient(connProp) {
"use strict";
this.user = connProp.user;
this.password = connProp.password;
this.database = connProp.database;
this.host = connProp.host;
this.port = connProp.port || DEFAULT_MYSQL_PORT;
this.autocommit = connProp.autocommit || DEFAULT_AUTOCOMMIT;
this.connectionLimit = connProp.connectionLimit || DEFAULT_CONNLIMIT
this.pool = null;
this.connProp = {
connectionLimit : this.connectionLimit,
host : this.host,
port : this.port,
user : this.user,
password : this.password,
database : this.database,
multipleStatements: true
};
}
mysqlc.MySqlClient.prototype.runQry = function runQry(qry, args, callback) {
console.log('running mysql qry:', qry);
this.pool.getConnection(function(err, connection) {
if (err) {
console.error('runQry-cannot getConnection ERROR:', err);
return callback(err);
}
connection.query(qry, args, function(err, rows) {
if (err) {
console.error('runQry-cannot run qry ERROR:', err);
return callback(err);
}
connection.release();
return callback(null, rows);
});
});
};
mysqlc.MySqlClient.prototype.connect = function connect(callback) {
var pool = mysql.createPool(this.connProp);
this.pool = pool;
pool.on('enqueue', function () {
console.error('runQry-Waiting for available connection slot-ERROR');
return callback('pool-not-empty');
});
// verify connection
pool.getConnection(function(err, connection) {
console.log('connecting mysql');
if (err) {
console.error('ERROR connecting to mysql server with connProp:', this.connProp);
return callback(err);
}
connection.release();
return callback(null);
});
};
mysqlc.MySqlClient.prototype.disconnect = function disconnect(callback) {
var pool = this.pool;
pool.getConnection(function(err, connection) {
console.log('disconnecting mysql');
if (err) {
console.error('ERROR disconnecting to mysql server with connProp:', this.connProp);
return callback(err);
}
connection.release();
pool.end();
return callback(null);
});
} |
Does it have any issues if you change the interval from |
Hmm.. The interval doesn't help, as the connections are still present for a long time. Its actually not anything to do with the interval, as this is only to simulate what happens when I use node-mysql with express-js. When I use it with express-js, I am never calling pool.end() throughout the period the server is up (many days)... Now, what I have noticed is, after couple of hours during the non-peak hours when there are no requests to the server, the mysql connections are released and But, during peak hours, when many users access the same page (which makes multiple calls to |
I think you are confused about what the If at peak times 10 is not enough for you, then you need to increase that limit. |
This module will hold on to those 10 connections for as long as possible, so that when a new request comes in, it doesn't have high latency waiting for a new connection to MySQL to be established. We don't close the connections at all, in fact--it's up to the MySQL server to close them, which happens after a few hours of inactivity, according to your MySQL server's configuration. |
TL;DR |
Yes, thanks, I understand it better. I knew that Yes, I will eventually increase the limit. |
There isn't an idle setting yet, but there is an open ticket: #962 Feel free to submit a PR if you need it quick :) Otherwise it will eventually come some day. Another option is you can ditch this module's pool implementation and use |
thank you :) |
Hi felixge,
Can you please take a look at this?
http://stackoverflow.com/questions/28575164/node-mysql-pool-active-connections-not-getting-released
Thanks for all your previous helps.
The text was updated successfully, but these errors were encountered: