@@ -402,54 +402,42 @@ public async Task<bool> ExecuteTransactionWithRetryAsync(ZoneTreeFactory<int, in
402
402
{
403
403
using var zoneTree = zoneTreeFactory .OpenOrCreateTransactional ();
404
404
var transactionId = zoneTree .BeginTransaction ();
405
- int retryCount = 0 ;
406
405
407
- while (retryCount <= maxRetries )
408
- {
409
- var result = zoneTree .UpsertNoThrow (transactionId , 1 , 100 );
410
- if (result .IsAborted )
411
- {
412
- // Abort the transaction and return false on failure.
413
- return false ;
414
- }
406
+ // Execute the operations within the transaction
407
+ var result = zoneTree .UpsertNoThrow (transactionId , 1 , 100 );
408
+ if (result .IsAborted )
409
+ return false ; // Abort the transaction and return false on failure.
415
410
416
- result = zoneTree .UpsertNoThrow (transactionId , 2 , 200 );
417
- if (result .IsAborted )
418
- {
419
- // Abort the transaction and return false on failure.
420
- return false ;
421
- }
411
+ result = zoneTree .UpsertNoThrow (transactionId , 2 , 200 );
412
+ if (result .IsAborted )
413
+ return false ; // Abort the transaction and return false on failure.
422
414
415
+ // Retry only the prepare step
416
+ int retryCount = 0 ;
417
+ while (retryCount <= maxRetries )
418
+ {
423
419
var prepareResult = zoneTree .PrepareNoThrow (transactionId );
424
420
if (prepareResult .IsAborted )
425
- {
426
- // Abort the transaction and return false on failure.
427
- return false ;
428
- }
421
+ return false ; // Abort the transaction and return false on failure.
429
422
430
423
if (prepareResult .IsPendingTransactions )
431
424
{
432
- // Optionally wait or handle pending transactions
433
425
await Task .Delay (100 ); // Simple delay before retrying
434
426
retryCount ++ ;
435
- continue ; // Retry the transaction
427
+ continue ; // Retry the prepare step
436
428
}
437
429
438
430
if (prepareResult .IsReadyToCommit )
439
- {
440
- var commitResult = zoneTree .CommitNoThrow (transactionId );
441
- if (commitResult .IsAborted )
442
- {
443
- // Abort the transaction and return false on failure.
444
- return false ;
445
- }
446
- // Transaction committed successfully
447
- return true ;
448
- }
431
+ break ; // Exit loop if ready to commit
449
432
}
450
433
451
- // Return false if retries are exhausted without a successful commit.
452
- return false ;
434
+ // After successfully preparing, commit the transaction
435
+ var commitResult = zoneTree .CommitNoThrow (transactionId );
436
+ if (commitResult .IsAborted )
437
+ return false ; // Abort the transaction and return false on failure.
438
+
439
+ // Transaction committed successfully
440
+ return true ;
453
441
}
454
442
```
455
443
0 commit comments