Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CompositeTransaction.cs
1using System;
2using System.Threading.Tasks;
3
5{
10 {
11 private readonly ITransaction[] transactions;
12 private readonly bool parallel;
13
20 public CompositeTransaction(Guid Id, bool Parallel, params ITransaction[] Transactions)
21 : base(Id)
22 {
23 if (Transactions is null)
24 throw new ArgumentNullException("Array of transactions cannot be null.");
25
26 this.transactions = Transactions;
27 this.parallel = Parallel;
28 }
29
33 protected override async Task<bool> DoPrepare()
34 {
35 if (this.parallel)
36 {
37 int i, c = this.transactions.Length;
38 Task<bool>[] Tasks = new Task<bool>[c];
39
40 for (i = 0; i < c; i++)
41 Tasks[i] = this.transactions[i].Prepare();
42
43 await Task.WhenAll(Tasks);
44
45 return And(Tasks);
46 }
47 else
48 {
49 foreach (ITransaction Transaction in this.transactions)
50 {
51 if (!await Transaction.Prepare())
52 return false;
53 }
54
55 return true;
56 }
57 }
58
59 private static bool And(Task<bool>[] Tasks)
60 {
61 foreach (Task<bool> Task in Tasks)
62 {
63 if (!Task.Result)
64 return false;
65 }
66
67 return true;
68 }
69
73 protected override async Task<bool> DoExecute()
74 {
75 if (this.parallel)
76 {
77 int i, c = this.transactions.Length;
78 Task<bool>[] Tasks = new Task<bool>[c];
79
80 for (i = 0; i < c; i++)
81 Tasks[i] = this.transactions[i].Execute();
82
83 await Task.WhenAll(Tasks);
84
85 return And(Tasks);
86 }
87 else
88 {
89 foreach (ITransaction Transaction in this.transactions)
90 {
91 if (!await Transaction.Execute())
92 return false;
93 }
94
95 return true;
96 }
97 }
98
102 protected override async Task<bool> DoCommit()
103 {
104 if (this.parallel)
105 {
106 int i, c = this.transactions.Length;
107 Task<bool>[] Tasks = new Task<bool>[c];
108
109 for (i = 0; i < c; i++)
110 Tasks[i] = this.transactions[i].Commit();
111
112 await Task.WhenAll(Tasks);
113
114 return And(Tasks);
115 }
116 else
117 {
118 foreach (ITransaction Transaction in this.transactions)
119 {
120 if (!await Transaction.Commit())
121 return false;
122 }
123
124 return true;
125 }
126 }
127
131 protected override async Task<bool> DoRollback()
132 {
133 if (this.parallel)
134 {
135 int i, c = this.transactions.Length;
136 Task[] Tasks = new Task[c];
137
138 for (i = 0; i < c; i++)
139 Tasks[i] = this.transactions[i].Abort();
140
141 await Task.WhenAll(Tasks);
142 }
143 else
144 {
145 foreach (ITransaction Transaction in this.transactions)
146 await Transaction.Abort();
147 }
148
149 return true;
150 }
151 }
152}
A transaction built up of a set of sub-transactions.
override async Task< bool > DoCommit()
Performs actual commit.
override async Task< bool > DoPrepare()
Performs actual preparation.
override async Task< bool > DoRollback()
Performs actual rollback.
CompositeTransaction(Guid Id, bool Parallel, params ITransaction[] Transactions)
A transaction built up of a set of sub-transactions.
override async Task< bool > DoExecute()
Performs actual execution.
Abstract base class for transactions.
Definition: Transaction.cs:17
async Task< bool > Commit()
Commits any changes made during the execution phase.
Definition: Transaction.cs:250
async Task< bool > Prepare()
Prepares the transaction for execution. This step can be used for validation and authorization of the...
Definition: Transaction.cs:114
async Task Abort()
Aborts the transaction.
Definition: Transaction.cs:390
async Task< bool > Execute()
Executes the transaction.
Definition: Transaction.cs:188
Maintains a collection of active transactions.
Definition: Transactions.cs:15
Interface for transactions
Definition: ITransaction.cs:10