pool.query fails after calling pool.end
See original GitHub issueSometimes, I need to close the old pool each 5 mins and create a new one. So I can’t make sure pool.end
is called after all queries end. Since it is said
The end method takes an optional callback that you can use to know once all the connections have ended. The connections end gracefully, so all pending queries will still complete and the time to end the pool will vary.
in the doc, I think I can just call pool.end
any time I want, but I will occasionally get an error:
Error: Pool is closed.
So,
What does pending queries mean in the doc?
Is a query which have been called pool.query
a pending query?
For example,
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : 'example.org',
user : 'bob',
password : 'secret',
database : 'my_db'
});
pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
pool.end(); // will be called somewhere else, but will raise the same error here
I’ve found this commit 76de66e2c36a6da71dcd010b3afe0029e5eb68bb fixed pool.getConnection race conditions, but I don’t quite understand what are the race conditions here. In my opinion, after I have called pool.query
, this query is a pending query no matter what phase the operation is under the hood such as ping.
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (8 by maintainers)
example:
error:
It may not raise an error the first time you run, but it will eventually if you run several times.
Thanks @HQidea , I can reproduce it.
So what’s happening is
pool.query()
first.