Operation - an atomic action that can finish successfully (expected) or unsuccessfully.
- Because of library is about distributed functionality, operation is an action that is aimed to add\update\delete data at another microservice.
- Operation interface is easy to understand - it encapsulates a
commitandrollbackmethods.
Task CommitAsync(CancellationToken cancellationToken);
Task RollbackAsync(CancellationToken cancellationToken);- Operation can exist in three states:
public enum OperationStatus
{
Committed,
NeedsToRollback,
FailedToRollback
Rollbacked
}
Transaction - a set of operations. They are grouped by the same transaction_type. Transaction can exist in these states:
public enum TransactionStatus
{
// successful flow
Created,
FinishedCorrectly,
// finally successful flow
FinishedWithRollback,
// unsuccessful flow
NeedsToRollback,
FailedToRollback
Failed
}Createdstands for a created transaction, but no operations were executed so far.FinishedCorrectlymeans that transaction has executed all of operations successfully.NeedsToRollbacktransaction stands for a transaction, to which some operations are bound, which are in the same state. So we definitely can try to queryOperationEntity[]from a database and try to rollback them.FailedToRollbacktransaction means it was inNeedsToRollbackstate, but failed to rollback. You need to be cautious maybe you have to manually rollback such operations because they can infinitely try to rollback and never ever succeed.FinishedWithRollbackmeans transaction has failed before N times, but finally at some moment it succeeded to rollback dataFailedmeans it was created, but the first operation has failed, so we could not rollback it, because no data needs to be rollback-ed. However there is no reason to leave transaction inCreatedstate, because we know it has failed.