@@ -54,6 +54,7 @@ class Transaction {
54
54
this . _onError = this . _onErrorCallback . bind ( this )
55
55
this . _onComplete = this . _onCompleteCallback . bind ( this )
56
56
this . _fetchSize = fetchSize
57
+ this . _results = [ ]
57
58
}
58
59
59
60
_begin ( bookmark , txConfig ) {
@@ -86,27 +87,30 @@ class Transaction {
86
87
parameters
87
88
)
88
89
89
- return this . _state . run ( query , params , {
90
+ var result = this . _state . run ( query , params , {
90
91
connectionHolder : this . _connectionHolder ,
91
92
onError : this . _onError ,
92
93
onComplete : this . _onComplete ,
93
94
reactive : this . _reactive ,
94
95
fetchSize : this . _fetchSize
95
96
} )
97
+ this . _results . push ( result )
98
+ return result
96
99
}
97
100
98
101
/**
99
102
* Commits the transaction and returns the result.
100
103
*
101
104
* After committing the transaction can no longer be used.
102
105
*
103
- * @returns {Result } New Result
106
+ * @returns {Promise<void> } An empty promise if committed successfully or error if any error happened during commit.
104
107
*/
105
108
commit ( ) {
106
109
const committed = this . _state . commit ( {
107
110
connectionHolder : this . _connectionHolder ,
108
111
onError : this . _onError ,
109
- onComplete : this . _onComplete
112
+ onComplete : this . _onComplete ,
113
+ pendingResults : this . _results
110
114
} )
111
115
this . _state = committed . state
112
116
// clean up
@@ -124,13 +128,15 @@ class Transaction {
124
128
*
125
129
* After rolling back, the transaction can no longer be used.
126
130
*
127
- * @returns {Result } New Result
131
+ * @returns {Promise<void> } An empty promise if rolled back successfully or error if any error happened during
132
+ * rollback.
128
133
*/
129
134
rollback ( ) {
130
135
const rolledback = this . _state . rollback ( {
131
136
connectionHolder : this . _connectionHolder ,
132
137
onError : this . _onError ,
133
- onComplete : this . _onComplete
138
+ onComplete : this . _onComplete ,
139
+ pendingResults : this . _results
134
140
} )
135
141
this . _state = rolledback . state
136
142
// clean up
@@ -170,15 +176,27 @@ class Transaction {
170
176
const _states = {
171
177
// The transaction is running with no explicit success or failure marked
172
178
ACTIVE : {
173
- commit : ( { connectionHolder, onError, onComplete } ) => {
179
+ commit : ( { connectionHolder, onError, onComplete, pendingResults } ) => {
174
180
return {
175
- result : finishTransaction ( true , connectionHolder , onError , onComplete ) ,
181
+ result : finishTransaction (
182
+ true ,
183
+ connectionHolder ,
184
+ onError ,
185
+ onComplete ,
186
+ pendingResults
187
+ ) ,
176
188
state : _states . SUCCEEDED
177
189
}
178
190
} ,
179
- rollback : ( { connectionHolder, onError, onComplete } ) => {
191
+ rollback : ( { connectionHolder, onError, onComplete, pendingResults } ) => {
180
192
return {
181
- result : finishTransaction ( false , connectionHolder , onError , onComplete ) ,
193
+ result : finishTransaction (
194
+ false ,
195
+ connectionHolder ,
196
+ onError ,
197
+ onComplete ,
198
+ pendingResults
199
+ ) ,
182
200
state : _states . ROLLED_BACK
183
201
}
184
202
} ,
@@ -188,14 +206,13 @@ const _states = {
188
206
{ connectionHolder, onError, onComplete, reactive, fetchSize }
189
207
) => {
190
208
// RUN in explicit transaction can't contain bookmarks and transaction configuration
209
+ // No need to include mode and database name as it shall be inclued in begin
191
210
const observerPromise = connectionHolder
192
211
. getConnection ( )
193
212
. then ( conn =>
194
213
conn . protocol ( ) . run ( statement , parameters , {
195
214
bookmark : Bookmark . empty ( ) ,
196
215
txConfig : TxConfig . empty ( ) ,
197
- mode : connectionHolder . mode ( ) ,
198
- database : connectionHolder . database ( ) ,
199
216
beforeError : onError ,
200
217
afterComplete : onComplete ,
201
218
reactive : reactive ,
@@ -356,22 +373,32 @@ const _states = {
356
373
* @param {ConnectionHolder } connectionHolder
357
374
* @param {function(err:Error): any } onError
358
375
* @param {function(metadata:object): any } onComplete
376
+ * @param {list<Result>> }pendingResults all run results in this transaction
359
377
*/
360
- function finishTransaction ( commit , connectionHolder , onError , onComplete ) {
378
+ function finishTransaction (
379
+ commit ,
380
+ connectionHolder ,
381
+ onError ,
382
+ onComplete ,
383
+ pendingResults
384
+ ) {
361
385
const observerPromise = connectionHolder
362
386
. getConnection ( )
363
387
. then ( connection => {
364
- if ( commit ) {
365
- return connection . protocol ( ) . commitTransaction ( {
366
- beforeError : onError ,
367
- afterComplete : onComplete
368
- } )
369
- } else {
370
- return connection . protocol ( ) . rollbackTransaction ( {
371
- beforeError : onError ,
372
- afterComplete : onComplete
373
- } )
374
- }
388
+ pendingResults . forEach ( r => r . summary ( ) )
389
+ return Promise . all ( pendingResults ) . then ( results => {
390
+ if ( commit ) {
391
+ return connection . protocol ( ) . commitTransaction ( {
392
+ beforeError : onError ,
393
+ afterComplete : onComplete
394
+ } )
395
+ } else {
396
+ return connection . protocol ( ) . rollbackTransaction ( {
397
+ beforeError : onError ,
398
+ afterComplete : onComplete
399
+ } )
400
+ }
401
+ } )
375
402
} )
376
403
. catch ( error => new FailedObserver ( { error, onError } ) )
377
404
0 commit comments