Pooling keeps incrementing connections and ends up in ER_CON_COUNT_ERROR
I have a class called Connection like one below. This only executes select statements. I have non pooling connection for insert or update.
var _mysql = require('mysql');
function Connection()
{
//private variables and dependencies includes
//create mysql pool connection requires nodejs mysql this connection is used only for selects.
var _connectionSelect = _mysql.createPool({
host : _config.mySQLDB.select.dbHost,
user : _config.mySQLDB.select.dbUser,
password : _config.mySQLDB.select.dbPass,
database : _config.mySQLDB.select.dbName,
supportBigNumbers : true,
connectTimeout : 7000,
connectionLimit : 5,
queueLimit : 5
});
this.executeSelect = function(sql, callback, Message)
{
//connects to mysql.
_connectionSelect.getConnection(function(connectionError, Connection){
if(connectionError)
{
console.log(connectionError);
//throws error if connection or sql gone wrong
Message.add("error", 'serviceDown');
Message.add("devError", 'unknownError');
callback(false);
}
else
{
//executes the query passed
Connection.query(sql, function(error, rows) {
Message.incerementQuery();
if(error)
{
Connection.release();
console.log(error+sql);
//throws error if connection or sql gone wrong
Message.add("error", 'unknownError');
Message.add("devError", "seriousError", "Database errors at resource server side");
callback(false);
}
else
{
Connection.release();
//executes the callback function
callback(rows);
}
});
}
});
};
}
exports.Connection = Connection;
I create an instance of this class whenever I want to execute a query.
I am aware that the default concurrent connections in MySQL is 100 and I wanted to keep that number.
Whenever I try running my application, this connection pooling is incrementing the for every select and reaches 100 connections pretty soon.
As you can see I am releasing the connection on success or error states. I am pretty sure that I must be doing something wrong, but difficult to figure out.
Is it because how I create instances of this class? I was hoping that if I supply
connectionLimit : 5
even if I create many instances of this class it should only utilise 5 connection?
Note: I have only one instance of this app in my local machine.
Sorry to be so amateur, I am new to this streaming I/O business. I love the idea of pooling but if I cant sort this out, I may need to use traditional open and close connection for every query . Any help would be much appreciated.
Many thanks, Karthik
I create an instance of this class whenever I want to execute a query.
This is why the connectionLimit is not being followed; every new instance of the Connection class you have above will have the 5 connection limit in it's own pool; if you make a new class instance whenever you execute a query, the connection limit is meaningless, because each instance will have it's own pool instance, thus the connection limit is not shared between them.
You need to have your little Connection class defined like this, instead:
var _mysql = require('mysql');
//create mysql pool connection requires nodejs mysql this connection is used only for selects.
var _connectionSelect = _mysql.createPool({
host : _config.mySQLDB.select.dbHost,
user : _config.mySQLDB.select.dbUser,
password : _config.mySQLDB.select.dbPass,
database : _config.mySQLDB.select.dbName,
supportBigNumbers : true,
connectTimeout : 7000,
connectionLimit : 5,
queueLimit : 5
});
function Connection()
{
//private variables and dependencies includes
this.executeSelect = function(sql, callback, Message)
{
//connects to mysql.
_connectionSelect.getConnection(function(connectionError, Connection){
if(connectionError)
{
console.log(connectionError);
//throws error if connection or sql gone wrong
Message.add("error", 'serviceDown');
Message.add("devError", 'unknownError');
callback(false);
}
else
{
//executes the query passed
Connection.query(sql, function(error, rows) {
Message.incerementQuery();
if(error)
{
Connection.release();
console.log(error+sql);
//throws error if connection or sql gone wrong
Message.add("error", 'unknownError');
Message.add("devError", "seriousError", "Database errors at resource server side");
callback(false);
}
else
{
Connection.release();
//executes the callback function
callback(rows);
}
});
}
});
};
}
exports.Connection = Connection;
Notice how I made the pool outside of your Connection class, so all the Connection classes share the same pool, thus limiting the number of connections to 5 like you intended.
I see, Thank you so much. It explains everything.
Many thanks, Karthik