diff --git a/lib/postgresql.js b/lib/postgresql.js index 0a538d8f..71b9368b 100644 --- a/lib/postgresql.js +++ b/lib/postgresql.js @@ -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); @@ -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); }); }; @@ -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) { @@ -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); }); } };