We call end() on the pool in order to shut down all the connections within the pool.
However, doing so prevents us from being able to use the object any longer.
Any attempt of calling .connect after .end() was called once results in the following error:
pool is draining and cannot accept work
Below is a complete test application:
'use strict';
var pg = require('pg');
var config = {
database: 'newone',
port: 5433,
user: 'postgres'
};
var pool = new pg.Pool(config);
function test1() {
pool.connect((err, client, done) => {
if (err) {
console.log('Error Connecting:', err);
return;
}
client.query('select count(*) from users', (err, data) => {
if (err) {
console.log('Query Error:', err);
} else {
console.log('DATA:', data.rows);
}
done();
pool.end();
test2();
});
});
}
function test2() {
pool.connect((err, client, done) => {
if (err) {
console.log('Error Connecting:', err);
return;
}
client.query('select count(*) from users', (err, data) => {
if (err) {
console.log('Query Error:', err);
} else {
console.log('DATA:', data.rows);
}
done();
pool.end();
});
});
}
test1();
test1 ends successfully, but in test2 it always throws that error on this line:
console.log('Error Connecting:', err);
//=> pool is draining and cannot accept work