public void Transaction(Action action) { lock (this.SyncLock) { this.AssertValid(); EnsureTransaction(); try { CurrentTransaction.Enlist(); action(); CurrentTransaction.Leave(); } catch (Exception) { CurrentTransaction.Rollback(); throw; } finally { if (!CurrentTransaction.Running) { CurrentTransaction = null; } } } }
/// <summary> /// Enlist in the transaction, enlisting consists only in passing a <see cref="Action<Boolean>"/> that /// will be called when the transaction will committ or rollback witha parameter of true and false respectively. /// </summary> /// <param name="completeTransactionCallback">The callback that will be called when the transaction will end, the /// value of the Boolean parameter will be true if the transaction should committ and false if the transaction /// should rollback.</param> /// <returns>A disposable action, if we are in a transaction the action does nothing, but /// if we are not in a transaction the action will committ or dispose the transaction.</returns> public static TransactionToken Enlist(Action <Boolean> completeTransactionCallback) { if (IsInTransaction) { //Let the current transaction handle this. CurrentTransaction.Enlist(completeTransactionCallback); return(new TransactionToken(TransactionsCount - 1)); } else { //We are not in a transaction return(new TransactionTokenForNoGlobalTransaction(completeTransactionCallback)); } }