@@ -3212,8 +3212,26 @@ class Database extends common.GrpcServiceObject {
32123212 ? ( optionsOrRunFn as RunTransactionOptions )
32133213 : { } ;
32143214
3215+ const retry = ( span : Span ) => {
3216+ this . runTransaction ( options , ( err , txn ) => {
3217+ if ( err ) {
3218+ setSpanError ( span , err ) ;
3219+ runFn ! ( err , null ) ;
3220+ return ;
3221+ }
3222+
3223+ txn ! . once ( 'end' , ( ) => {
3224+ span . end ( ) ;
3225+ } ) ;
3226+ txn ! . once ( 'error' , ( ) => {
3227+ span . end ( ) ;
3228+ } ) ;
3229+ runFn ! ( null , txn ! ) ;
3230+ } ) ;
3231+ } ;
3232+
32153233 startTrace ( 'Database.runTransaction' , this . _traceConfig , span => {
3216- this . pool_ . getSession ( async ( err , session ?, transaction ?) => {
3234+ this . pool_ . getSession ( ( err , session ?, transaction ?) => {
32173235 if ( err ) {
32183236 setSpanError ( span , err ) ;
32193237 }
@@ -3222,11 +3240,7 @@ class Database extends common.GrpcServiceObject {
32223240 span . addEvent ( 'No session available' , {
32233241 'session.id' : session ?. id ,
32243242 } ) ;
3225- // In this case we are invoking runTransaction afresh
3226- // hence we have to wait for this call to complete before
3227- // ending the span.
3228- await this . runTransaction ( options , runFn ! ) ;
3229- span . end ( ) ;
3243+ retry ( span ) ;
32303244 return ;
32313245 }
32323246
@@ -3246,24 +3260,26 @@ class Database extends common.GrpcServiceObject {
32463260 transaction ! . excludeTxnFromChangeStreams ( ) ;
32473261 }
32483262
3263+ // Our span should only be ended if the
3264+ // transaction either errored or was ended.
3265+ transaction ! . once ( 'error' , err => {
3266+ setSpanError ( span , err ! ) ;
3267+ span . end ( ) ;
3268+ } ) ;
3269+
3270+ transaction ! . once ( 'end' , err => {
3271+ setSpanError ( span , err ! ) ;
3272+ span . end ( ) ;
3273+ } ) ;
3274+
32493275 const release = ( ) => {
32503276 this . pool_ . release ( session ! ) ;
32513277 } ;
32523278
32533279 const runner = new TransactionRunner (
32543280 session ! ,
32553281 transaction ! ,
3256- async ( err , resp ) => {
3257- if ( err ) {
3258- setSpanError ( span , err ! ) ;
3259- }
3260- // It is paramount that we await
3261- // the caller to return before
3262- // exiting this function otherwise the span
3263- // order will not be correct.
3264- await runFn ! ( err , resp ) ;
3265- span . end ( ) ;
3266- } ,
3282+ runFn ! ,
32673283 options
32683284 ) ;
32693285
@@ -3275,13 +3291,12 @@ class Database extends common.GrpcServiceObject {
32753291 'session.id' : session ?. id ,
32763292 } ) ;
32773293 release ( ) ;
3278- await this . runTransaction ( options , runFn ! ) ;
3294+ retry ( span ) ;
32793295 } else {
32803296 setImmediate ( runFn ! , err ) ;
32813297 release ( ) ;
3298+ span . end ( ) ;
32823299 }
3283-
3284- span . end ( ) ;
32853300 } ) ;
32863301 } ) ;
32873302 } ) ;
0 commit comments