@@ -34,20 +34,24 @@ class Transaction {
34
34
* @param {ConnectionHolder } connectionHolder - the connection holder to get connection from.
35
35
* @param {function() } onClose - Function to be called when transaction is committed or rolled back.
36
36
* @param {function(bookmark: Bookmark) } onBookmark callback invoked when new bookmark is produced.
37
+ * @param onConnection callback invoked when a connection is obtained from connection holder. This need to be
38
+ * called to see if the connection is in the process of being released.
37
39
*/
38
- constructor ( connectionHolder , onClose , onBookmark ) {
40
+ constructor ( connectionHolder , onClose , onBookmark , onConnection ) {
39
41
this . _connectionHolder = connectionHolder
40
42
this . _state = _states . ACTIVE
41
43
this . _onClose = onClose
42
44
this . _onBookmark = onBookmark
45
+ this . _onConnection = onConnection
43
46
}
44
47
45
48
_begin ( bookmark , txConfig ) {
46
49
const streamObserver = new _TransactionStreamObserver ( this )
47
50
48
51
this . _connectionHolder
49
52
. getConnection ( streamObserver )
50
- . then ( conn =>
53
+ . then ( conn => {
54
+ this . _onConnection ( )
51
55
conn
52
56
. protocol ( )
53
57
. beginTransaction (
@@ -56,7 +60,7 @@ class Transaction {
56
60
this . _connectionHolder . mode ( ) ,
57
61
streamObserver
58
62
)
59
- )
63
+ } )
60
64
. catch ( error => streamObserver . onError ( error ) )
61
65
}
62
66
@@ -78,7 +82,8 @@ class Transaction {
78
82
this . _connectionHolder ,
79
83
new _TransactionStreamObserver ( this ) ,
80
84
query ,
81
- params
85
+ params ,
86
+ this . _onConnection
82
87
)
83
88
}
84
89
@@ -90,9 +95,10 @@ class Transaction {
90
95
* @returns {Result } New Result
91
96
*/
92
97
commit ( ) {
93
- let committed = this . _state . commit (
98
+ const committed = this . _state . commit (
94
99
this . _connectionHolder ,
95
- new _TransactionStreamObserver ( this )
100
+ new _TransactionStreamObserver ( this ) ,
101
+ this . _onConnection
96
102
)
97
103
this . _state = committed . state
98
104
// clean up
@@ -108,9 +114,10 @@ class Transaction {
108
114
* @returns {Result } New Result
109
115
*/
110
116
rollback ( ) {
111
- let committed = this . _state . rollback (
117
+ const committed = this . _state . rollback (
112
118
this . _connectionHolder ,
113
- new _TransactionStreamObserver ( this )
119
+ new _TransactionStreamObserver ( this ) ,
120
+ this . _onConnection
114
121
)
115
122
this . _state = committed . state
116
123
// clean up
@@ -161,29 +168,40 @@ class _TransactionStreamObserver extends StreamObserver {
161
168
}
162
169
163
170
/** internal state machine of the transaction */
164
- let _states = {
171
+ const _states = {
165
172
// The transaction is running with no explicit success or failure marked
166
173
ACTIVE : {
167
- commit : ( connectionHolder , observer ) => {
174
+ commit : ( connectionHolder , observer , onConnection ) => {
168
175
return {
169
- result : finishTransaction ( true , connectionHolder , observer ) ,
176
+ result : finishTransaction (
177
+ true ,
178
+ connectionHolder ,
179
+ observer ,
180
+ onConnection
181
+ ) ,
170
182
state : _states . SUCCEEDED
171
183
}
172
184
} ,
173
- rollback : ( connectionHolder , observer ) => {
185
+ rollback : ( connectionHolder , observer , onConnection ) => {
174
186
return {
175
- result : finishTransaction ( false , connectionHolder , observer ) ,
187
+ result : finishTransaction (
188
+ false ,
189
+ connectionHolder ,
190
+ observer ,
191
+ onConnection
192
+ ) ,
176
193
state : _states . ROLLED_BACK
177
194
}
178
195
} ,
179
- run : ( connectionHolder , observer , statement , parameters ) => {
196
+ run : ( connectionHolder , observer , statement , parameters , onConnection ) => {
180
197
// RUN in explicit transaction can't contain bookmarks and transaction configuration
181
198
const bookmark = Bookmark . empty ( )
182
199
const txConfig = TxConfig . empty ( )
183
200
184
201
connectionHolder
185
202
. getConnection ( observer )
186
- . then ( conn =>
203
+ . then ( conn => {
204
+ onConnection ( )
187
205
conn
188
206
. protocol ( )
189
207
. run (
@@ -194,7 +212,7 @@ let _states = {
194
212
connectionHolder . mode ( ) ,
195
213
observer
196
214
)
197
- )
215
+ } )
198
216
. catch ( error => observer . onError ( error ) )
199
217
200
218
return _newRunResult ( observer , statement , parameters , ( ) =>
@@ -206,7 +224,7 @@ let _states = {
206
224
// An error has occurred, transaction can no longer be used and no more messages will
207
225
// be sent for this transaction.
208
226
FAILED : {
209
- commit : ( connectionHolder , observer ) => {
227
+ commit : ( connectionHolder , observer , onConnection ) => {
210
228
observer . onError ( {
211
229
error :
212
230
'Cannot commit statements in this transaction, because previous statements in the ' +
@@ -218,14 +236,14 @@ let _states = {
218
236
state : _states . FAILED
219
237
}
220
238
} ,
221
- rollback : ( connectionHolder , observer ) => {
239
+ rollback : ( connectionHolder , observer , onConnection ) => {
222
240
observer . markCompleted ( )
223
241
return {
224
242
result : _newDummyResult ( observer , 'ROLLBACK' , { } ) ,
225
243
state : _states . FAILED
226
244
}
227
245
} ,
228
- run : ( connectionHolder , observer , statement , parameters ) => {
246
+ run : ( connectionHolder , observer , statement , parameters , onConnection ) => {
229
247
observer . onError ( {
230
248
error :
231
249
'Cannot run statement, because previous statements in the ' +
@@ -237,7 +255,7 @@ let _states = {
237
255
238
256
// This transaction has successfully committed
239
257
SUCCEEDED : {
240
- commit : ( connectionHolder , observer ) => {
258
+ commit : ( connectionHolder , observer , onConnection ) => {
241
259
observer . onError ( {
242
260
error :
243
261
'Cannot commit statements in this transaction, because commit has already been successfully called on the transaction and transaction has been closed. Please start a new' +
@@ -248,7 +266,7 @@ let _states = {
248
266
state : _states . SUCCEEDED
249
267
}
250
268
} ,
251
- rollback : ( connectionHolder , observer ) => {
269
+ rollback : ( connectionHolder , observer , onConnection ) => {
252
270
observer . onError ( {
253
271
error :
254
272
'Cannot rollback transaction, because transaction has already been successfully closed.'
@@ -258,7 +276,7 @@ let _states = {
258
276
state : _states . SUCCEEDED
259
277
}
260
278
} ,
261
- run : ( connectionHolder , observer , statement , parameters ) => {
279
+ run : ( connectionHolder , observer , statement , parameters , onConnection ) => {
262
280
observer . onError ( {
263
281
error :
264
282
'Cannot run statement, because transaction has already been successfully closed.'
@@ -269,7 +287,7 @@ let _states = {
269
287
270
288
// This transaction has been rolled back
271
289
ROLLED_BACK : {
272
- commit : ( connectionHolder , observer ) => {
290
+ commit : ( connectionHolder , observer , onConnection ) => {
273
291
observer . onError ( {
274
292
error :
275
293
'Cannot commit this transaction, because it has already been rolled back.'
@@ -279,7 +297,7 @@ let _states = {
279
297
state : _states . ROLLED_BACK
280
298
}
281
299
} ,
282
- rollback : ( connectionHolder , observer ) => {
300
+ rollback : ( connectionHolder , observer , onConnection ) => {
283
301
observer . onError ( {
284
302
error :
285
303
'Cannot rollback transaction, because transaction has already been rolled back.'
@@ -289,7 +307,7 @@ let _states = {
289
307
state : _states . ROLLED_BACK
290
308
}
291
309
} ,
292
- run : ( connectionHolder , observer , statement , parameters ) => {
310
+ run : ( connectionHolder , observer , statement , parameters , onConnection ) => {
293
311
observer . onError ( {
294
312
error :
295
313
'Cannot run statement, because transaction has already been rolled back.'
@@ -299,10 +317,11 @@ let _states = {
299
317
}
300
318
}
301
319
302
- function finishTransaction ( commit , connectionHolder , observer ) {
320
+ function finishTransaction ( commit , connectionHolder , observer , onConnection ) {
303
321
connectionHolder
304
322
. getConnection ( observer )
305
323
. then ( connection => {
324
+ onConnection ( )
306
325
if ( commit ) {
307
326
return connection . protocol ( ) . commitTransaction ( observer )
308
327
} else {
0 commit comments