8
8
9
9
/*jslint unparam: true */
10
10
11
+ var CONN_TIMEOUT = 5000 ;
12
+ var DISCONN_TIMEOUT = 5000 ;
13
+
11
14
var handleConnectionFailure = function ( api , connection , actionTemplate , error , next ) {
12
15
api . log ( "Close all opened connections" , "debug" ) ;
13
16
var connectionClosedCount = 0 ;
@@ -57,6 +60,13 @@ exports.transaction = function (api, next) {
57
60
transactionPreProcessor = function ( connection , actionTemplate , next ) {
58
61
if ( actionTemplate . transaction === "read" || actionTemplate . transaction === "write" ) {
59
62
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 ) ;
60
70
61
71
actionTemplate . databases . forEach ( function ( databaseName ) {
62
72
dbConnection = api . dataAccess . createConnection ( databaseName ) ;
@@ -68,12 +78,14 @@ exports.transaction = function (api, next) {
68
78
callback = function ( err , result ) {
69
79
connection . dbConnectionMap = dbConnectionMap ;
70
80
if ( err ) {
81
+ clearTimeout ( clearMe ) ;
71
82
handleConnectionFailure ( api , connection , actionTemplate , err , next ) ;
72
83
return ;
73
84
}
74
85
75
86
connectionOpenedCount += 1 ;
76
87
if ( connectionOpenedCount === actionTemplate . databases . length ) {
88
+ clearTimeout ( clearMe ) ;
77
89
api . log ( "All connections are opened" , "debug" ) ;
78
90
next ( connection , true ) ;
79
91
}
@@ -121,6 +133,14 @@ exports.transaction = function (api, next) {
121
133
* @param {Function } next - The callback function
122
134
*/
123
135
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
+
124
144
var connectionClosedCount = 0 ;
125
145
if ( connection . dbConnectionMap !== null && connection . dbConnectionMap !== undefined && actionTemplate . transaction !== null && actionTemplate . transaction !== undefined ) {
126
146
actionTemplate . databases . forEach ( function ( databaseName ) {
@@ -129,13 +149,15 @@ exports.transaction = function (api, next) {
129
149
connection . dbConnectionMap [ databaseName ] . disconnect ( ) ;
130
150
api . log ( "Connection is closed" , "debug" ) ;
131
151
if ( err ) {
152
+ clearTimeout ( clearMe ) ;
132
153
connection . error = err ;
133
154
next ( connection ) ;
134
155
return ;
135
156
}
136
157
137
158
connectionClosedCount += 1 ;
138
159
if ( connectionClosedCount === actionTemplate . databases . length ) {
160
+ clearTimeout ( clearMe ) ;
139
161
api . log ( "All connections are closed" , "debug" ) ;
140
162
next ( connection ) ;
141
163
}
0 commit comments