88
99/*jslint unparam: true */
1010
11+ var CONN_TIMEOUT = process . env . CONN_TIMEOUT || 5000 ;
12+ var DISCONNECT_ON_CONN_TIMEOUT = process . env . DISCONNECT_ON_CONN_TIMEOUT !== "false" ? true : false ;
13+ var DISCONN_TIMEOUT = process . env . DISCONN_TIMEOUT || 5000 ;
14+
1115var handleConnectionFailure = function ( api , connection , actionTemplate , error , next ) {
1216 api . log ( "Close all opened connections" , "debug" ) ;
1317 var connectionClosedCount = 0 ;
1418 actionTemplate . databases . forEach ( function ( databaseName ) {
1519 var callback ;
1620 callback = function ( err , result ) {
1721 connection . dbConnectionMap [ databaseName ] . disconnect ( ) ;
18- api . log ( "Connection is closed" , "debug" ) ;
22+ api . log ( "Connection is closed for " + databaseName , "debug" ) ;
1923 if ( err ) {
2024 connection . error = err ;
2125 next ( connection , false ) ;
@@ -57,6 +61,15 @@ exports.transaction = function (api, next) {
5761 transactionPreProcessor = function ( connection , actionTemplate , next ) {
5862 if ( actionTemplate . transaction === "read" || actionTemplate . transaction === "write" ) {
5963 var dbConnectionMap = { } , dbConnection , callback , connectionOpenedCount = 0 ;
64+
65+ var connectTimeout = function ( ) {
66+ api . log ( "Timed out without obtaining all DB connections" , "error" ) ;
67+ if ( DISCONNECT_ON_CONN_TIMEOUT ) {
68+ handleConnectionFailure ( api , connection , actionTemplate , "Open Timeout" , next ) ;
69+ }
70+ }
71+
72+ var clearMe = setTimeout ( connectTimeout , CONN_TIMEOUT ) ;
6073
6174 actionTemplate . databases . forEach ( function ( databaseName ) {
6275 dbConnection = api . dataAccess . createConnection ( databaseName ) ;
@@ -68,12 +81,14 @@ exports.transaction = function (api, next) {
6881 callback = function ( err , result ) {
6982 connection . dbConnectionMap = dbConnectionMap ;
7083 if ( err ) {
84+ clearTimeout ( clearMe ) ;
7185 handleConnectionFailure ( api , connection , actionTemplate , err , next ) ;
7286 return ;
7387 }
7488
7589 connectionOpenedCount += 1 ;
7690 if ( connectionOpenedCount === actionTemplate . databases . length ) {
91+ clearTimeout ( clearMe ) ;
7792 api . log ( "All connections are opened" , "debug" ) ;
7893 next ( connection , true ) ;
7994 }
@@ -121,6 +136,14 @@ exports.transaction = function (api, next) {
121136 * @param {Function } next - The callback function
122137 */
123138 transactionPostProcessor = function ( connection , actionTemplate , toRender , next ) {
139+
140+ var disconnectTimeout = function ( ) {
141+ api . error ( "Timed out without closing all DB connections" , "error" ) ;
142+ // I dont want to call next(connection); here because I want to allow the execution to to continue in case connection can be closed after timeout
143+ }
144+
145+ var clearMe = setTimeout ( disconnectTimeout , DISCONN_TIMEOUT ) ;
146+
124147 var connectionClosedCount = 0 ;
125148 if ( connection . dbConnectionMap !== null && connection . dbConnectionMap !== undefined && actionTemplate . transaction !== null && actionTemplate . transaction !== undefined ) {
126149 actionTemplate . databases . forEach ( function ( databaseName ) {
@@ -129,13 +152,15 @@ exports.transaction = function (api, next) {
129152 connection . dbConnectionMap [ databaseName ] . disconnect ( ) ;
130153 api . log ( "Connection is closed" , "debug" ) ;
131154 if ( err ) {
155+ clearTimeout ( clearMe ) ;
132156 connection . error = err ;
133157 next ( connection ) ;
134158 return ;
135159 }
136160
137161 connectionClosedCount += 1 ;
138162 if ( connectionClosedCount === actionTemplate . databases . length ) {
163+ clearTimeout ( clearMe ) ;
139164 api . log ( "All connections are closed" , "debug" ) ;
140165 next ( connection ) ;
141166 }
0 commit comments