@@ -69,7 +69,7 @@ function PostgreSQL(postgresql, settings) {
69
69
// this.settings = settings;
70
70
this . constructor . super_ . call ( this , 'postgresql' , settings ) ;
71
71
this . clientConfig = settings . url || settings ;
72
- this . pg = postgresql ;
72
+ this . pg = new postgresql . Pool ( this . clientConfig ) ;
73
73
this . settings = settings ;
74
74
if ( settings . debug ) {
75
75
debug ( 'Settings %j' , settings ) ;
@@ -95,7 +95,7 @@ PostgreSQL.prototype.getDefaultSchemaName = function() {
95
95
*/
96
96
PostgreSQL . prototype . connect = function ( callback ) {
97
97
var self = this ;
98
- self . pg . connect ( self . clientConfig , function ( err , client , done ) {
98
+ self . pg . connect ( function ( err , client , done ) {
99
99
self . client = client ;
100
100
process . nextTick ( done ) ;
101
101
callback && callback ( err , client ) ;
@@ -130,9 +130,12 @@ PostgreSQL.prototype.executeSQL = function(sql, params, options, callback) {
130
130
self . debug ( err ) ;
131
131
}
132
132
if ( self . settings . debug && data ) self . debug ( "%j" , data ) ;
133
- process . nextTick ( function ( ) {
134
- done ( err ) ;
135
- } ) ;
133
+ if ( done ) {
134
+ process . nextTick ( function ( ) {
135
+ // Release the connection in next tick
136
+ done ( err ) ;
137
+ } ) ;
138
+ }
136
139
var result = null ;
137
140
if ( data ) {
138
141
switch ( data . command ) {
@@ -152,10 +155,10 @@ PostgreSQL.prototype.executeSQL = function(sql, params, options, callback) {
152
155
if ( transaction && transaction . connection &&
153
156
transaction . connector === this ) {
154
157
debug ( 'Execute SQL within a transaction' ) ;
155
- executeWithConnection ( transaction . connection ,
156
- transaction . connection . release ) ;
158
+ // Do not release the connection
159
+ executeWithConnection ( transaction . connection , null ) ;
157
160
} else {
158
- self . pg . connect ( self . clientConfig , function ( err , connection , done ) {
161
+ self . pg . connect ( function ( err , connection , done ) {
159
162
if ( err ) return callback ( err ) ;
160
163
executeWithConnection ( connection , done ) ;
161
164
} ) ;
0 commit comments