-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
pool.query fails after calling pool.end #1803
Comments
Hi @HQidea , the That is why your callback functions throw the error because the new queries can not find a connection for the pool is closed. |
@HQidea , could you close this issue? |
Hi @elemount, does this count as an official response? |
Sorry, I'm not sure what's your problem? Could you clarify it? |
To a user, there isn't any document contains the definition of |
"Error: Pool is closed." is not an error from one of the queries, it's "you are trying to close pool that is already closed" type of error. When you call function refreshPool() {
const poolToClose = pool;
pool = null;
if (poolToClose) {
poolToClose.end();
}
pool = mysql.createPool(config);
} |
Hi @sidorares, I don't think the error is from I have similar mechanism as yours, but the error is still there because After If Lines 190 to 218 in e8fea70
|
I can see there could potentially be a race (not sure if it's actually happening) @HQidea would you be able to make simplest possible self contained example to show this problem? |
example: const mysql = require('mysql');
class Driver {
constructor() {
this.pool = null;
}
getPool() {
if (!this.pool) {
const pool = this.pool = mysql.createPool({
host: '',
port: 3306,
user: '',
password: '',
database: ''
});
setTimeout(() => {
this.pool = null;
pool.end();
}, 1000);
}
return this.pool;
}
query(cb) {
this.getPool().query('show tables', cb);
}
}
const driver = new Driver();
function call() {
driver.query((err, data) => {
if (err) {
console.log(err);
return;
}
setTimeout(() => {
call();
}, Math.random() * 1000);
});
}
call(); 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
|
So, sorry to come late, but looks like there is two issues here: (1) the docs do not describe well the current behavior and (2) you would like to change the current behavior. I'll write up the docs for (1) and @HQidea you're welcome to make a pull request for (2) to do what you're looking for. The behavior you're seeing isn't a bug, rather it is designed like that, but you're welcome to implement a pull request that adds a new option to implement the behavior you're looking for :) ! |
Ok, I tried to improve the docs. If you have any additional wording suggestions, please feel free to pull request those changes as well :) ! |
Sometimes, 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 saidin the doc, I think I can just call
pool.end
any time I want, but I will occasionally get an error:So,
What does pending queries mean in the doc?
Is a query which have been called
pool.query
a pending query?For example,
I've found this commit 76de66e 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.The text was updated successfully, but these errors were encountered: