@@ -74,11 +74,11 @@ function query(msg,connection,statement,params,done,error) {
74
74
return ;
75
75
}
76
76
const pool = getPool ( connector . pool ) ;
77
-
77
+
78
78
if ( logger . active ) logger . send ( { label :"query " , connection :connection , prepare :this . prepareSQL , preparable :pool . preparable } ) ;
79
79
if ( this . prepareSQL && pool . preparable ) {
80
80
const node = this ;
81
- pool . prepare ( connector ,
81
+ pool . prepare ( connector ,
82
82
( prepared ) => pool . exec ( prepared , done , error , params ) ,
83
83
( err ) => {
84
84
if ( logger . active ) logger . send ( { label :"query prepare" , error :err } ) ;
@@ -99,7 +99,7 @@ function query(msg,connection,statement,params,done,error) {
99
99
params
100
100
) ;
101
101
return ;
102
- }
102
+ }
103
103
if ( logger . active ) logger . send ( "query connection all connections" ) ;
104
104
if ( this . prepareSQL ) {
105
105
const node = this ;
@@ -143,7 +143,7 @@ function batch(msg,connection,statement,params,done,error) {
143
143
params
144
144
) ;
145
145
return ;
146
- }
146
+ }
147
147
if ( logger . active ) logger . send ( "query connection all connections" ) ;
148
148
connectionProcessor . apply ( this , [ msg , "batch" , done , error , statement , params ] ) ;
149
149
}
@@ -169,7 +169,7 @@ function release(msg,done,error) {
169
169
cmProcessor . apply ( this , [ msg , "release" , done , error ] ) ;
170
170
return ;
171
171
}
172
- const thisObject = this ,
172
+ const thisObject = this ,
173
173
action = this . rollbackTransaction ?"rollback" :"commit" ;
174
174
cmProcessor . apply ( this , [ msg , action ,
175
175
( ) => {
@@ -229,7 +229,7 @@ function ConnectionPool(node) {
229
229
logger . sendError ( "ConnectionPool " + ex . message ) ;
230
230
}
231
231
}
232
- ConnectionPool . prototype . batch = function ( c , done , error , sql , params ) {
232
+ ConnectionPool . prototype . batch = function ( c , done , error , sql , params ) {
233
233
if ( logger . active ) logger . send ( { label :"ConnectionPool batch" , id :c . id , sql :sql , parms :params , prepareid :prepareid } ) ;
234
234
this . lastUsed [ c . id ] = new Date ( ) ;
235
235
const pool = this ;
@@ -298,7 +298,7 @@ ConnectionPool.prototype.error=function(err,callback) {
298
298
this . lastError = err ;
299
299
callback ( err ) ;
300
300
}
301
- ConnectionPool . prototype . exec = function ( c , done , error , id , params ) {
301
+ ConnectionPool . prototype . exec = function ( c , done , error , id , params ) {
302
302
if ( logger . active ) logger . send ( "ConnectionPool exec connection id: " + c . id + " prepared id: " + id + " params: " + JSON . stringify ( params ) ) ;
303
303
if ( ! this . preparable ) {
304
304
this . query ( c , done , error , this . prepared [ c . id ] [ id ] , params , id ) ;
@@ -323,7 +323,7 @@ ConnectionPool.prototype.getConnection=function(done,error) {
323
323
return ;
324
324
}
325
325
if ( logger . active ) logger . send ( "ConnectionPool getConnection create new connection" ) ;
326
- if ( ++ this . newConnections > this . size ) { // this.pool.length updated too late
326
+ if ( ++ this . newConnections > this . size ) { // this.pool.length updated too late
327
327
connectionPool . error ( "maximum pool size " + this . size , error ) ;
328
328
return ;
329
329
}
@@ -347,7 +347,7 @@ ConnectionPool.prototype.getConnection=function(done,error) {
347
347
ConnectionPool . prototype . getDetails = function ( ) {
348
348
return { connected :this . pool . length , active :this . active . length , free :this . free . length , lastError :this . lastError || '' , autoCommit :this . autoCommit } ;
349
349
}
350
- ConnectionPool . prototype . prepare = function ( c , done , error , sql , id ) {
350
+ ConnectionPool . prototype . prepare = function ( c , done , error , sql , id ) {
351
351
if ( logger . active ) logger . send ( "ConnectionPool prepare connection id: " + c . id + " prepare id: " + id + " sql: " + sql ) ;
352
352
if ( this . prepared [ c . id ] ) {
353
353
if ( logger . active ) logger . send ( "ConnectionPool prepare already done" ) ;
@@ -377,13 +377,13 @@ ConnectionPool.prototype.prepare=function(c,done,error,sql,id) {
377
377
pool . prepared [ c . id ] [ id ] = prepared ;
378
378
if ( logger . active ) logger . send ( "ConnectionPool prepared calling done" ) ;
379
379
done ( prepared ) ;
380
- } ,
380
+ } ,
381
381
( err ) => {
382
382
if ( logger . active ) logger . send ( "ConnectionPool prepare " + err ) ;
383
383
error ( err )
384
384
} ) ;
385
385
} ;
386
- ConnectionPool . prototype . query = function ( c , done , error , sql , params , prepareid ) {
386
+ ConnectionPool . prototype . query = function ( c , done , error , sql , params , prepareid ) {
387
387
if ( logger . active ) logger . send ( { label :"ConnectionPool query connection" , id :c . id , sql :sql , parms :params , prepareid :prepareid } ) ;
388
388
this . lastUsed [ c . id ] = new Date ( ) ;
389
389
const pool = this ;
@@ -424,7 +424,7 @@ ConnectionPool.prototype.rollback=function(c,done,error) {
424
424
pool . checkDeadConnection ( c , err ) ;
425
425
error ( err )
426
426
} ) ;
427
- }
427
+ }
428
428
ConnectionPool . prototype . releaseStaleConnections = function ( ) {
429
429
try {
430
430
const thisObject = this ,
@@ -462,16 +462,22 @@ ConnectionPool.prototype.releaseFreeConnections=function() {
462
462
}
463
463
ConnectionPool . prototype . test = function ( done , error ) {
464
464
if ( this . connectionTested ) done ( ) ;
465
+ const thisObject = this ;
465
466
this . getConnection (
466
467
( conn ) => {
467
- conn . pool . release ( conn . c , ( ) => {
468
- conn . pool . connectionTested = true ;
469
- done ( ) ;
470
- } ,
471
- error
472
- )
468
+ try {
469
+ thisObject . release ( conn . c , ( ) => {
470
+ conn . pool . connectionTested = true ;
471
+ done ( ) ;
472
+ } ,
473
+ error
474
+ )
475
+ } catch ( ex ) {
476
+ logger . sendError ( "ConnectionPool.test error: " + ex ) ;
477
+ error ( ex ) ;
478
+ }
473
479
}
474
- , error ) ;
480
+ , error ) ;
475
481
}
476
482
477
483
module . exports = function ( RED ) {
@@ -486,7 +492,7 @@ module.exports = function(RED) {
486
492
node . connectionError = err ;
487
493
} ) ;
488
494
node . testConnection = function ( done , error ) {
489
- if ( node . connectionError ) error ( node . connectionError ) ;
495
+ if ( node . connectionError ) error ( node . connectionError ) ;
490
496
else done ( ) ;
491
497
}
492
498
node . setMsg = function ( msg , done , error ) {
@@ -518,7 +524,7 @@ module.exports = function(RED) {
518
524
) ;
519
525
} ;
520
526
node . on ( "close" , function ( removed , done ) {
521
- clearInterval ( node . releaseStaleConnections ) ;
527
+ clearInterval ( node . releaseStaleConnections ) ;
522
528
node . connectionPool . close ( done ) ;
523
529
} ) ;
524
530
node . releaseStaleConnections = setInterval ( function ( node ) { node . connectionPool . releaseStaleConnections . apply ( node . connectionPool ) } , 1000 * 60 , node ) ;
@@ -535,10 +541,10 @@ function Driver(a) {
535
541
if ( logger . active ) logger . send ( { label :"New Drive " , argument :a } ) ;
536
542
if ( ! a . optionsMapping ) {
537
543
this . optionsMapping = {
538
- host : "host" ,
539
- port : "port" ,
540
- database : "dbname" ,
541
- user : "user" ,
544
+ host : "host" ,
545
+ port : "port" ,
546
+ database : "dbname" ,
547
+ user : "user" ,
542
548
password : "password"
543
549
} ;
544
550
}
@@ -562,7 +568,7 @@ function Driver(a) {
562
568
Driver . prototype . batchArrayNotSupported = function ( pool , conn , sql , params , done , error , i , results , errors , prepared ) {
563
569
error ( "Driver " + this . driverName + " doesn't support batch array" ) ;
564
570
if ( i && i > params . length ) {
565
- if ( errors )
571
+ if ( errors )
566
572
done ( results ) ;
567
573
return
568
574
} else {
@@ -571,7 +577,7 @@ Driver.prototype.batchArrayNotSupported=function(pool,conn,sql,params,done,error
571
577
this . driver . prepare ( pool , conn , sql ,
572
578
( prepared ) => {
573
579
this . batchArrayNotSupported ( pool , conn , sql , params , done , error , 0 , results , errors , prepared ) ;
574
- } ,
580
+ } ,
575
581
( err ) => {
576
582
if ( logger . active ) logger . send ( "batchArrayNotSupported prepare " + err ) ;
577
583
error ( err ) ;
@@ -700,22 +706,27 @@ Driver.prototype.getOptions=function(node) {
700
706
}
701
707
return this . optionsCached ;
702
708
} ;
709
+
710
+ Driver . prototype . toStringConnection = function ( node ) {
711
+ return JSON . stringify ( Object . assign ( { } , this . getOptions ( node ) , { password :"***masked" } ) )
712
+ } ;
713
+
703
714
Driver . prototype . getConnectionC = function ( pool , node , done , error ) {
704
715
try {
705
716
const options = this . getOptions ( node ) ;
706
- if ( logger . active ) logger . send ( "getConnectionC options " + JSON . stringify ( Object . assign ( { } , options , { password : "***masked" } ) ) ) ;
707
- const thisObject = this ;
708
- const c = new ( this . Driver ( ) ) ( options ) ;
717
+ if ( logger . active ) logger . send ( "getConnectionC options " + this . toStringConnection ( node ) ) ;
718
+ const thisObject = this , Driver = this . Driver ( ) ;
719
+ const c = new Driver ( options ) ;
709
720
c . connect ( ( err ) => {
710
721
if ( err ) {
711
- if ( logger . active ) logger . send ( "getConnectionC error " + err ) ;
722
+ if ( logger . active ) logger . send ( "getConnectionC options: " + this . toStringConnection ( node ) + " error "+ err ) ;
712
723
error ( err ) ;
713
724
return ;
714
725
}
715
726
if ( logger . active ) logger . send ( "getConnectionC OK " ) ;
716
727
if ( thisObject . testOnConnect ) {
717
728
if ( ! thisObject . onConnectCache ) {
718
- const mustache = require ( "mustache" ) ;
729
+ const mustache = require ( "mustache" ) ;
719
730
thisObject . onConnectCache = mustache . render ( thisObject . testOnConnect , node ) ;
720
731
}
721
732
thisObject . query ( pool , c , thisObject . onConnectCache , null , ( ) => done ( c ) , error ) ;
@@ -724,16 +735,16 @@ Driver.prototype.getConnectionC=function(pool,node,done,error) {
724
735
}
725
736
} ) ;
726
737
} catch ( e ) {
727
- logger . sendError ( "Driver.getConnectionC error: " + e ) ;
738
+ logger . sendError ( "Driver.getConnectionC options: " + this . toStringConnection ( node ) + " error: "+ e ) ;
728
739
error ( e ) ;
729
740
}
730
741
} ;
731
742
Driver . prototype . getConnectionO = function ( pool , node , done , error ) {
732
743
try {
733
744
const options = this . getOptions ( node ) ;
734
- if ( logger . active ) logger . send ( "getConnectionO options " + JSON . stringify ( Object . assign ( { } , options , { password : "***masked" } ) ) ) ;
745
+ if ( logger . active ) logger . send ( "getConnectionO options " + this . toStringConnection ( node ) ) ;
735
746
if ( ! this . driverInstance ) this . driverInstance = this . Driver ( ) ;
736
- let thisObject = this ,
747
+ const thisObject = this ,
737
748
connectString = "DATABASE=" + options . database + ";HOSTNAME=" + options . host + ";UID=" + options . user + ";PWD=" + options . password + ";PORT=" + options . port + ";PROTOCOL=TCPIP" ;
738
749
// var ibmdb = require('ibm_db');
739
750
// ibmdb.open(connectString,(err,conn)=>{
@@ -1107,10 +1118,10 @@ const DriverType = {
1107
1118
prepare :Driver . prototype . prepareQ ,
1108
1119
exec :Driver . prototype . execQ ,
1109
1120
optionsMapping : {
1110
- host : "host" ,
1111
- port : "port" ,
1112
- dbname : "dbname" ,
1113
- user : "user" ,
1121
+ host : "host" ,
1122
+ port : "port" ,
1123
+ dbname : "dbname" ,
1124
+ user : "user" ,
1114
1125
password : "password"
1115
1126
} ,
1116
1127
prepareIsQuery :false ,
@@ -1130,12 +1141,12 @@ const DriverType = {
1130
1141
testOnConnect :null ,
1131
1142
getConnection : Driver . prototype . getConnectionNeo4j ,
1132
1143
query :Driver . prototype . queryNeo4j
1133
- } ) ,
1144
+ } ) ,
1134
1145
'pg' : new Driver ( {
1135
- // Driver:(()=>{const { Client } = require('pg'); return Client}),
1146
+ Driver :( ( ) => { const { Client } = require ( 'pg' ) ; return Client } ) ,
1136
1147
requireName :'pg' ,
1137
1148
autoCommit :true ,
1138
- connectOptions :{ "connectionTimeoutMillis" : 2000 } ,
1149
+ connectOptions :{ "connectionTimeoutMillis" : 4000 } ,
1139
1150
getResultArray :( ( r ) => r . rows ) ,
1140
1151
prepareIsQuery :true ,
1141
1152
query :Driver . prototype . queryCPG ,
@@ -1146,4 +1157,4 @@ const DriverType = {
1146
1157
} ;
1147
1158
function ArrayObjects2ArrayArray ( v ) {
1148
1159
return v . map ( ( row ) => Object . values ( row ) ) ;
1149
- }
1160
+ }
0 commit comments