@@ -359,42 +359,40 @@ func (d *Datastore) lowlevelTransactionSet(ctx context.Context, transaction *typ
359359
360360func (d * Datastore ) TransactionSet (ctx context.Context , transactionId string , transactionIntents []* types.TransactionIntent , replaceIntent * types.TransactionIntent , transactionTimeout time.Duration , dryRun bool ) (* sdcpb.TransactionSetResponse , error ) {
361361 var err error
362+ var transaction * types.Transaction
363+ var transactionGuard * types.TransactionGuard
362364
363365 log .Infof ("Transaction: %s - start" , transactionId )
364- // try locking the datastore if it is locked return the specific ErrDatastoreLocked error.
365- if ! d .dmutex .TryLock () {
366- log .Infof ("Transaction: %s - abort (%v)" , transactionId , ErrDatastoreLocked )
367- return nil , ErrDatastoreLocked
368- }
369- defer d .dmutex .Unlock ()
370366
371367 // create a new Transaction with the given transaction id
372- transaction : = types .NewTransaction (transactionId , d .transactionManager )
368+ transaction = types .NewTransaction (transactionId , d .transactionManager )
373369 // set the timeout on the transaction
374370 transaction .SetTimeout (transactionTimeout )
375371
376- var transactionGuard * types.TransactionGuard
377-
378- // Try to register the Transaction in the TransactionManager only a single transaction can be register (implicitly being active)
379- for {
380- select {
381- case <- ctx .Done ():
382- // Context was canceled or timed out
383- log .Errorf ("Transaction: %s - context canceled or timed out: %v" , transactionId , ctx .Err ())
384- return nil , ErrContextDone
385- default :
386- // Start a transaction and prepare to cancel it if any error occurs
387- transactionGuard , err = d .transactionManager .RegisterTransaction (ctx , transaction )
388- if transactionGuard != nil {
389- defer transactionGuard .Done ()
390- break
391- }
392- // log.Warnf("Transaction: %s - failed to create transaction, retrying: %v", transactionId, err)
393- // time.Sleep(time.Millisecond * 200)
372+ if ! dryRun {
373+ // try locking the datastore if it is locked return the specific ErrDatastoreLocked error.
374+ if ! d .dmutex .TryLock () {
375+ log .Infof ("Transaction: %s - abort (%v)" , transactionId , ErrDatastoreLocked )
394376 return nil , ErrDatastoreLocked
395377 }
396- if transactionGuard != nil {
397- break
378+ defer d .dmutex .Unlock ()
379+
380+ // Try to register the Transaction in the TransactionManager only a single transaction can be register (implicitly being active)
381+ for {
382+ select {
383+ case <- ctx .Done ():
384+ // Context was canceled or timed out
385+ log .Errorf ("Transaction: %s - context canceled or timed out: %v" , transactionId , ctx .Err ())
386+ return nil , ErrContextDone
387+ default :
388+ // Start a transaction and prepare to cancel it if any error occurs
389+ transactionGuard , err = d .transactionManager .RegisterTransaction (ctx , transaction )
390+ if transactionGuard != nil {
391+ defer transactionGuard .Done ()
392+ break
393+ }
394+ return nil , ErrDatastoreLocked
395+ }
398396 }
399397 }
400398
@@ -432,9 +430,12 @@ func (d *Datastore) TransactionSet(ctx context.Context, transactionId string, tr
432430 }
433431
434432 // Mark the transaction as successfully committed
435- transactionGuard .Success ()
433+ if ! dryRun {
434+ // Mark the transaction as successfully committed
435+ transactionGuard .Success ()
436436
437- log .Infof ("Transaction: %s - transacted" , transactionId )
437+ log .Infof ("Transaction: %s - transacted" , transactionId )
438+ }
438439 return response , err
439440}
440441
0 commit comments