node-mysql2: Pool not releasing connections if maxIdle is not set
All of my queries include dbConnection.release(), however the connections do not get released and once connectionLimit is reached, the execution pauses waiting for an available connection.
I’ve re-worked my code so that every pool.getConnection() has a dbConnection.release() right next to it, to make absolutely certain that I’m not accidentally leaving any connections unreleased.
However, this did not help, and it seems that there is a bug in the releasing of pool connections.
I store my pool in a static variable with a class called Global.
class globals
{
static dbPool = null;
}
Runs once on startup
globals.dbPool = await mysql.createPool({
host: databaseServer,
port: databasePort,
user: databaseUsername,
password: databasePassword,
database: databaseName,
connectionLimit: 10
});
Example usage
const dbConnection = await globals.dbPool.getConnection();
let mysqlResponse = null;
await dbConnection.query(`SELECT * FROM guild;`).then(result => {
mysqlResponse = result[0];
}).catch(err => console.log(err));
dbConnection.release();
Result after some queries have been executed

About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 2
- Comments: 18 (1 by maintainers)
I’m sorry. Let me clarify my fix…
In order to get it to cleanup old released connections, I had to set maxIdle < connectionLimit. Without this, my connections maxed out for no reason, and I ended up with 10 idle connections, and no further queries would execute.
If maxIdle is default (eg. maxIdle => connectionLimit), no cleanup of old released connections occurs as shown below.
https://github.com/sidorares/node-mysql2/blob/b87ee4456556712cb8b0538f2f264d9a9fe5764a/lib/pool.js#L30
Unfortunately, I cannot create a demo repo. I’ve never posted code to github and wouldn’t know where to start.
I would like to provide my company’s experience with this issue:
We noticed a high level of timeouts and failures, and ran across this issue. We can an experiment across some of our services.
maxIdle = 1, and noticed 13 timeouts (a rate of 1 every 1.33 days).maxIdle = 1fixed the problem.I suppose it could be related to something else, but @wellwelwel or @sidorares I have the same question as @dtimofeev:
I’m worried this has a much larger impact than may be assumed.
If we have maxIdle setting equal to connectionLimit (which is also the default configuration if maxIdle is not manually set) then the cleanup process will not be initiated at all. This means that connection exceeding their idleTimeout will never be closed/destroyed.
The only workaround I see at the moment is to explicitly configure maxIdle to connectionLimit - 1 in order for the cleanup to start at all.
@wellwelwel, shouldn’t _removeIdleTimeoutConnections be called in Pool constructor, regardless of the maxIdle is lower than the connectionLimit?
This happens to me too. I’m having exactly the same problem. Here is my codes;
db.js
users-repo.js
SomeFunc output before getAllUsers function
SomeFunc output after getAllUsers function
SomeFunc output after some time has passed
In that test, Try removing maxIdle so it’s default. Then run the test, pause in the middle for at least 5 seconds to allow idleTimeout to be reached, and then see if it releases the connections that have reached the idleTimeout. Also see if you can create a 6th connection after the idleTimeout has been reached on the first 5 connections. It should allow a 6th connection if the first 5 exceed their idleTimeout. I’d try it myself but I dont know how to run those tests.