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