Skip to content

Commit 2b2183d

Browse files
committed
Update sample.
1 parent 8667cbc commit 2b2183d

File tree

2 files changed

+42
-66
lines changed

2 files changed

+42
-66
lines changed

README.md

+21-33
Original file line numberDiff line numberDiff line change
@@ -402,54 +402,42 @@ public async Task<bool> ExecuteTransactionWithRetryAsync(ZoneTreeFactory<int, in
402402
{
403403
using var zoneTree = zoneTreeFactory.OpenOrCreateTransactional();
404404
var transactionId = zoneTree.BeginTransaction();
405-
int retryCount = 0;
406405

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.
415410
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.
422414
415+
// Retry only the prepare step
416+
int retryCount = 0;
417+
while (retryCount <= maxRetries)
418+
{
423419
var prepareResult = zoneTree.PrepareNoThrow(transactionId);
424420
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.
429422
430423
if (prepareResult.IsPendingTransactions)
431424
{
432-
// Optionally wait or handle pending transactions
433425
await Task.Delay(100); // Simple delay before retrying
434426
retryCount++;
435-
continue; // Retry the transaction
427+
continue; // Retry the prepare step
436428
}
437429

438430
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
449432
}
450433

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;
453441
}
454442
```
455443

src/ZoneTree/docs/ZoneTree/README-NUGET.md

+21-33
Original file line numberDiff line numberDiff line change
@@ -401,54 +401,42 @@ public async Task<bool> ExecuteTransactionWithRetryAsync(ZoneTreeFactory<int, in
401401
{
402402
using var zoneTree = zoneTreeFactory.OpenOrCreateTransactional();
403403
var transactionId = zoneTree.BeginTransaction();
404-
int retryCount = 0;
405404

406-
while (retryCount <= maxRetries)
407-
{
408-
var result = zoneTree.UpsertNoThrow(transactionId, 1, 100);
409-
if (result.IsAborted)
410-
{
411-
// Abort the transaction and return false on failure.
412-
return false;
413-
}
405+
// Execute the operations within the transaction
406+
var result = zoneTree.UpsertNoThrow(transactionId, 1, 100);
407+
if (result.IsAborted)
408+
return false; // Abort the transaction and return false on failure.
414409
415-
result = zoneTree.UpsertNoThrow(transactionId, 2, 200);
416-
if (result.IsAborted)
417-
{
418-
// Abort the transaction and return false on failure.
419-
return false;
420-
}
410+
result = zoneTree.UpsertNoThrow(transactionId, 2, 200);
411+
if (result.IsAborted)
412+
return false; // Abort the transaction and return false on failure.
421413
414+
// Retry only the prepare step
415+
int retryCount = 0;
416+
while (retryCount <= maxRetries)
417+
{
422418
var prepareResult = zoneTree.PrepareNoThrow(transactionId);
423419
if (prepareResult.IsAborted)
424-
{
425-
// Abort the transaction and return false on failure.
426-
return false;
427-
}
420+
return false; // Abort the transaction and return false on failure.
428421
429422
if (prepareResult.IsPendingTransactions)
430423
{
431-
// Optionally wait or handle pending transactions
432424
await Task.Delay(100); // Simple delay before retrying
433425
retryCount++;
434-
continue; // Retry the transaction
426+
continue; // Retry the prepare step
435427
}
436428

437429
if (prepareResult.IsReadyToCommit)
438-
{
439-
var commitResult = zoneTree.CommitNoThrow(transactionId);
440-
if (commitResult.IsAborted)
441-
{
442-
// Abort the transaction and return false on failure.
443-
return false;
444-
}
445-
// Transaction committed successfully
446-
return true;
447-
}
430+
break; // Exit loop if ready to commit
448431
}
449432

450-
// Return false if retries are exhausted without a successful commit.
451-
return false;
433+
// After successfully preparing, commit the transaction
434+
var commitResult = zoneTree.CommitNoThrow(transactionId);
435+
if (commitResult.IsAborted)
436+
return false; // Abort the transaction and return false on failure.
437+
438+
// Transaction committed successfully
439+
return true;
452440
}
453441
```
454442

0 commit comments

Comments
 (0)