@@ -54,6 +54,7 @@ class Transaction {
5454 this . _onError = this . _onErrorCallback . bind ( this )
5555 this . _onComplete = this . _onCompleteCallback . bind ( this )
5656 this . _fetchSize = fetchSize
57+ this . _results = [ ]
5758 }
5859
5960 _begin ( bookmark , txConfig ) {
@@ -86,27 +87,30 @@ class Transaction {
8687 parameters
8788 )
8889
89- return this . _state . run ( query , params , {
90+ var result = this . _state . run ( query , params , {
9091 connectionHolder : this . _connectionHolder ,
9192 onError : this . _onError ,
9293 onComplete : this . _onComplete ,
9394 reactive : this . _reactive ,
9495 fetchSize : this . _fetchSize
9596 } )
97+ this . _results . push ( result )
98+ return result
9699 }
97100
98101 /**
99102 * Commits the transaction and returns the result.
100103 *
101104 * After committing the transaction can no longer be used.
102105 *
103- * @returns {Result } New Result
106+ * @returns {Promise<void> } An empty promise if committed successfully or error if any error happened during commit.
104107 */
105108 commit ( ) {
106109 const committed = this . _state . commit ( {
107110 connectionHolder : this . _connectionHolder ,
108111 onError : this . _onError ,
109- onComplete : this . _onComplete
112+ onComplete : this . _onComplete ,
113+ pendingResults : this . _results
110114 } )
111115 this . _state = committed . state
112116 // clean up
@@ -124,13 +128,15 @@ class Transaction {
124128 *
125129 * After rolling back, the transaction can no longer be used.
126130 *
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.
128133 */
129134 rollback ( ) {
130135 const rolledback = this . _state . rollback ( {
131136 connectionHolder : this . _connectionHolder ,
132137 onError : this . _onError ,
133- onComplete : this . _onComplete
138+ onComplete : this . _onComplete ,
139+ pendingResults : this . _results
134140 } )
135141 this . _state = rolledback . state
136142 // clean up
@@ -170,15 +176,27 @@ class Transaction {
170176const _states = {
171177 // The transaction is running with no explicit success or failure marked
172178 ACTIVE : {
173- commit : ( { connectionHolder, onError, onComplete } ) => {
179+ commit : ( { connectionHolder, onError, onComplete, pendingResults } ) => {
174180 return {
175- result : finishTransaction ( true , connectionHolder , onError , onComplete ) ,
181+ result : finishTransaction (
182+ true ,
183+ connectionHolder ,
184+ onError ,
185+ onComplete ,
186+ pendingResults
187+ ) ,
176188 state : _states . SUCCEEDED
177189 }
178190 } ,
179- rollback : ( { connectionHolder, onError, onComplete } ) => {
191+ rollback : ( { connectionHolder, onError, onComplete, pendingResults } ) => {
180192 return {
181- result : finishTransaction ( false , connectionHolder , onError , onComplete ) ,
193+ result : finishTransaction (
194+ false ,
195+ connectionHolder ,
196+ onError ,
197+ onComplete ,
198+ pendingResults
199+ ) ,
182200 state : _states . ROLLED_BACK
183201 }
184202 } ,
@@ -188,14 +206,13 @@ const _states = {
188206 { connectionHolder, onError, onComplete, reactive, fetchSize }
189207 ) => {
190208 // 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
191210 const observerPromise = connectionHolder
192211 . getConnection ( )
193212 . then ( conn =>
194213 conn . protocol ( ) . run ( statement , parameters , {
195214 bookmark : Bookmark . empty ( ) ,
196215 txConfig : TxConfig . empty ( ) ,
197- mode : connectionHolder . mode ( ) ,
198- database : connectionHolder . database ( ) ,
199216 beforeError : onError ,
200217 afterComplete : onComplete ,
201218 reactive : reactive ,
@@ -356,22 +373,32 @@ const _states = {
356373 * @param {ConnectionHolder } connectionHolder
357374 * @param {function(err:Error): any } onError
358375 * @param {function(metadata:object): any } onComplete
376+ * @param {list<Result>> }pendingResults all run results in this transaction
359377 */
360- function finishTransaction ( commit , connectionHolder , onError , onComplete ) {
378+ function finishTransaction (
379+ commit ,
380+ connectionHolder ,
381+ onError ,
382+ onComplete ,
383+ pendingResults
384+ ) {
361385 const observerPromise = connectionHolder
362386 . getConnection ( )
363387 . 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+ } )
375402 } )
376403 . catch ( error => new FailedObserver ( { error, onError } ) )
377404
0 commit comments