Skip to content

Commit 98bccd2

Browse files
authored
Merge pull request #402 from STRML/fix/conn-release
fix(pool): synchronously release pool connection after query results Fix/conn release
2 parents 2e99944 + 72a012e commit 98bccd2

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

lib/postgresql.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ exports.initialize = function initializeDataSource(dataSource, callback) {
4040

4141
if (callback) {
4242
if (dbSettings.lazyConnect) {
43-
process.nextTick(function() {
44-
callback();
45-
});
43+
process.nextTick(callback);
4644
} else {
4745
dataSource.connecting = true;
4846
dataSource.connector.connect(callback);
@@ -97,9 +95,9 @@ PostgreSQL.prototype.getDefaultSchemaName = function() {
9795
*/
9896
PostgreSQL.prototype.connect = function(callback) {
9997
const self = this;
100-
self.pg.connect(function(err, client, done) {
98+
self.pg.connect(function(err, client, releaseCb) {
10199
self.client = client;
102-
process.nextTick(done);
100+
process.nextTick(releaseCb);
103101
callback && callback(err, client);
104102
});
105103
};
@@ -123,17 +121,13 @@ PostgreSQL.prototype.executeSQL = function(sql, params, options, callback) {
123121
debug('SQL: %s', sql);
124122
}
125123

126-
function executeWithConnection(connection, done) {
124+
function executeWithConnection(connection, releaseCb) {
127125
connection.query(sql, params, function(err, data) {
128126
// if(err) console.error(err);
129127
if (err) debug(err);
130128
if (data) debugData('%j', data);
131-
if (done) {
132-
process.nextTick(function() {
133-
// Release the connection in next tick
134-
done(err);
135-
});
136-
}
129+
// Release the connection back to the pool.
130+
if (releaseCb) releaseCb(err);
137131
let result = null;
138132
if (data) {
139133
switch (data.command) {
@@ -169,9 +163,9 @@ PostgreSQL.prototype.executeSQL = function(sql, params, options, callback) {
169163
// Do not release the connection
170164
executeWithConnection(transaction.connection, null);
171165
} else {
172-
self.pg.connect(function(err, connection, done) {
166+
self.pg.connect(function(err, connection, releaseCb) {
173167
if (err) return callback(err);
174-
executeWithConnection(connection, done);
168+
executeWithConnection(connection, releaseCb);
175169
});
176170
}
177171
};

0 commit comments

Comments
 (0)