Skip to content

Commit

Permalink
Merge pull request #402 from STRML/fix/conn-release
Browse files Browse the repository at this point in the history
fix(pool): synchronously release pool connection after query results Fix/conn release
  • Loading branch information
bajtos authored Dec 9, 2019
2 parents 2e99944 + 72a012e commit 98bccd2
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions lib/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ exports.initialize = function initializeDataSource(dataSource, callback) {

if (callback) {
if (dbSettings.lazyConnect) {
process.nextTick(function() {
callback();
});
process.nextTick(callback);
} else {
dataSource.connecting = true;
dataSource.connector.connect(callback);
Expand Down Expand Up @@ -97,9 +95,9 @@ PostgreSQL.prototype.getDefaultSchemaName = function() {
*/
PostgreSQL.prototype.connect = function(callback) {
const self = this;
self.pg.connect(function(err, client, done) {
self.pg.connect(function(err, client, releaseCb) {
self.client = client;
process.nextTick(done);
process.nextTick(releaseCb);
callback && callback(err, client);
});
};
Expand All @@ -123,17 +121,13 @@ PostgreSQL.prototype.executeSQL = function(sql, params, options, callback) {
debug('SQL: %s', sql);
}

function executeWithConnection(connection, done) {
function executeWithConnection(connection, releaseCb) {
connection.query(sql, params, function(err, data) {
// if(err) console.error(err);
if (err) debug(err);
if (data) debugData('%j', data);
if (done) {
process.nextTick(function() {
// Release the connection in next tick
done(err);
});
}
// Release the connection back to the pool.
if (releaseCb) releaseCb(err);
let result = null;
if (data) {
switch (data.command) {
Expand Down Expand Up @@ -169,9 +163,9 @@ PostgreSQL.prototype.executeSQL = function(sql, params, options, callback) {
// Do not release the connection
executeWithConnection(transaction.connection, null);
} else {
self.pg.connect(function(err, connection, done) {
self.pg.connect(function(err, connection, releaseCb) {
if (err) return callback(err);
executeWithConnection(connection, done);
executeWithConnection(connection, releaseCb);
});
}
};
Expand Down

0 comments on commit 98bccd2

Please sign in to comment.