88
99/*jslint unparam: true */
1010
11+ var CONN_TIMEOUT = 5000 ;
12+ var DISCONN_TIMEOUT = 5000 ;
13+
1114var handleConnectionFailure = function ( api , connection , actionTemplate , error , next ) {
1215 api . log ( "Close all opened connections" , "debug" ) ;
1316 var connectionClosedCount = 0 ;
@@ -57,6 +60,13 @@ exports.transaction = function (api, next) {
5760 transactionPreProcessor = function ( connection , actionTemplate , next ) {
5861 if ( actionTemplate . transaction === "read" || actionTemplate . transaction === "write" ) {
5962 var dbConnectionMap = { } , dbConnection , callback , connectionOpenedCount = 0 ;
63+
64+ var connectTimeout = function ( ) {
65+ api . log ( "Timed out without obtaining all DB connections" , "error" ) ;
66+ handleConnectionFailure ( api , connection , actionTemplate , "Open Timeout" , next ) ;
67+ }
68+
69+ var clearMe = setTimeout ( connectTimeout , CONN_TIMEOUT ) ;
6070
6171 actionTemplate . databases . forEach ( function ( databaseName ) {
6272 dbConnection = api . dataAccess . createConnection ( databaseName ) ;
@@ -68,12 +78,14 @@ exports.transaction = function (api, next) {
6878 callback = function ( err , result ) {
6979 connection . dbConnectionMap = dbConnectionMap ;
7080 if ( err ) {
81+ clearTimeout ( clearMe ) ;
7182 handleConnectionFailure ( api , connection , actionTemplate , err , next ) ;
7283 return ;
7384 }
7485
7586 connectionOpenedCount += 1 ;
7687 if ( connectionOpenedCount === actionTemplate . databases . length ) {
88+ clearTimeout ( clearMe ) ;
7789 api . log ( "All connections are opened" , "debug" ) ;
7890 next ( connection , true ) ;
7991 }
@@ -121,6 +133,14 @@ exports.transaction = function (api, next) {
121133 * @param {Function } next - The callback function
122134 */
123135 transactionPostProcessor = function ( connection , actionTemplate , toRender , next ) {
136+
137+ var disconnectTimeout = function ( ) {
138+ api . error ( "Timed out without closing all DB connections" , "error" ) ;
139+ // 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
140+ }
141+
142+ var clearMe = setTimeout ( disconnectTimeout , DISCONN_TIMEOUT ) ;
143+
124144 var connectionClosedCount = 0 ;
125145 if ( connection . dbConnectionMap !== null && connection . dbConnectionMap !== undefined && actionTemplate . transaction !== null && actionTemplate . transaction !== undefined ) {
126146 actionTemplate . databases . forEach ( function ( databaseName ) {
@@ -129,13 +149,15 @@ exports.transaction = function (api, next) {
129149 connection . dbConnectionMap [ databaseName ] . disconnect ( ) ;
130150 api . log ( "Connection is closed" , "debug" ) ;
131151 if ( err ) {
152+ clearTimeout ( clearMe ) ;
132153 connection . error = err ;
133154 next ( connection ) ;
134155 return ;
135156 }
136157
137158 connectionClosedCount += 1 ;
138159 if ( connectionClosedCount === actionTemplate . databases . length ) {
160+ clearTimeout ( clearMe ) ;
139161 api . log ( "All connections are closed" , "debug" ) ;
140162 next ( connection ) ;
141163 }
0 commit comments