Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Transactions.cs
1using System;
2using System.Threading.Tasks;
3using Waher.Events;
6
8{
13 public class Transactions<T> : ITransactions
14 where T : ITransaction
15 {
16 private readonly Cache<Guid, T> transactions;
17
22 public Transactions(TimeSpan TransactionTimeout)
23 {
24 this.transactions = new Cache<Guid, T>(int.MaxValue, TransactionTimeout, TransactionTimeout, true);
25 this.transactions.Removed += this.Transactions_Removed;
26
28 }
29
30 private async Task Transactions_Removed(object Sender, CacheItemEventArgs<Guid, T> e)
31 {
32 if (e.Reason != RemovedReason.Manual)
33 {
34 try
35 {
36 T Transaction = e.Value;
37
38 if (Transaction.State != TransactionState.Committed &&
40 {
41 await Transaction.Abort();
42 }
43 }
44 catch (Exception ex)
45 {
46 Log.Exception(ex);
47 }
48 }
49 }
50
54 public void Dispose()
55 {
57
58 this.transactions.Removed -= this.Transactions_Removed;
59 this.transactions.Clear();
60 this.transactions.Dispose();
61 }
62
70 public T2 CreateNew<T2>(params object[] Arguments)
71 where T2 : T
72 {
73 T2 Transaction = Types.Instantiate<T2>(false, Arguments);
74 this.transactions.Add(Transaction.Id, Transaction);
75 return Transaction;
76 }
77
82 public void Register(T Transaction)
83 {
84 this.transactions.Add(Transaction.Id, Transaction);
85 }
86
92 public bool Unregister(Guid Id)
93 {
94 return this.transactions.Remove(Id);
95 }
96
102 public bool Unregister(T Transaction)
103 {
104 if (this.transactions.TryGetValue(Transaction.Id, out T Transaction2) &&
105 Transaction2.Equals(Transaction))
106 {
107 return this.transactions.Remove(Transaction.Id);
108 }
109 else
110 return false;
111 }
112
119 public bool TryGetTransaction(Guid Id, out T Transaction)
120 {
121 return this.transactions.TryGetValue(Id, out Transaction);
122 }
123
129 public async Task<bool> Prepare(Guid TransactionId)
130 {
131 try
132 {
133 if (!this.transactions.TryGetValue(TransactionId, out T Transaction))
134 return false;
135
136 return await Transaction.Prepare();
137 }
138 catch (Exception ex)
139 {
140 Log.Exception(ex);
141 return false;
142 }
143 }
144
150 public async Task<bool> Execute(Guid TransactionId)
151 {
152 try
153 {
154 if (!this.transactions.TryGetValue(TransactionId, out T Transaction))
155 return false;
156
157 return await Transaction.Execute();
158 }
159 catch (Exception ex)
160 {
161 Log.Exception(ex);
162 return false;
163 }
164 }
165
171 public async Task<bool> Commit(Guid TransactionId)
172 {
173 try
174 {
175 if (!this.transactions.TryGetValue(TransactionId, out T Transaction))
176 return false;
177
178 if (!await Transaction.Commit())
179 return false;
180
181 this.transactions.Remove(TransactionId);
182
183 return true;
184 }
185 catch (Exception ex)
186 {
187 Log.Exception(ex);
188 return false;
189 }
190 }
191
197 public async Task<bool> Rollback(Guid TransactionId)
198 {
199 try
200 {
201 if (!this.transactions.TryGetValue(TransactionId, out T Transaction))
202 return false;
203
204 if (!await Transaction.Rollback())
205 return false;
206
207 this.transactions.Remove(TransactionId);
208
209 return true;
210 }
211 catch (Exception ex)
212 {
213 Log.Exception(ex);
214 return false;
215 }
216 }
217
221 public Task<ITransaction[]> GetTransactions()
222 {
223 T[] Transactions = this.transactions.GetValues();
224 int i, c = Transactions.Length;
225 ITransaction[] Result = new ITransaction[c];
226
227 for (i = 0; i < c; i++)
228 Result[i] = (ITransaction)Transactions[i];
229
230 return Task.FromResult<ITransaction[]>(Result);
231 }
232 }
233}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
Implements an in-memory cache.
Definition: Cache.cs:15
Event arguments for cache item removal events.
ValueType Value
Value of item that was removed.
RemovedReason Reason
Reason for removing the item.
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static object Instantiate(Type Type, params object[] Arguments)
Returns an instance of the type Type . If one needs to be created, it is. If the constructor requires...
Definition: Types.cs:1353
Abstract base class for transactions.
Definition: Transaction.cs:17
TransactionState State
Transaction state.
Definition: Transaction.cs:49
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
async Task< bool > Rollback()
Rolls back any changes made during the execution phase.
Definition: Transaction.cs:313
Module making sure no unfinished transactions are left when system ends.
static void Register(ITransactions Transactions)
Registers a collection of transactions with the module.
static bool Unregister(ITransactions Transactions)
Unregisters a collection of transactions with the module.
Maintains a collection of active transactions.
Definition: Transactions.cs:15
bool TryGetTransaction(Guid Id, out T Transaction)
Tries to get a transaction, given its ID.
Transactions(TimeSpan TransactionTimeout)
Maintains a collection of active transactions.
Definition: Transactions.cs:22
void Register(T Transaction)
Register a transaction created elsewhere with the collection.
Definition: Transactions.cs:82
bool Unregister(T Transaction)
Unregisters a transaction.
async Task< bool > Execute(Guid TransactionId)
Executes a transaction in the collection.
T2 CreateNew< T2 >(params object[] Arguments)
Creates a new transaction
Definition: Transactions.cs:70
async Task< bool > Prepare(Guid TransactionId)
Prepares a transaction in the collection.
void Dispose()
Rolls back any pending transactions and disposes of the object.
Definition: Transactions.cs:54
async Task< bool > Rollback(Guid TransactionId)
Prepares a transaction in the collection.
async Task< bool > Commit(Guid TransactionId)
Cimmits a transaction in the collection.
Task< ITransaction[]> GetTransactions()
Gets pending transactions.
bool Unregister(Guid Id)
Unregisters a transaction.
Definition: Transactions.cs:92
Interface for transactions
Definition: ITransaction.cs:10
Interface for collections of transactions that can be monitored by TransactionModule.
RemovedReason
Reason for removing the item.
TransactionState
State of a transaction