static async Task CommittableTransactionAsync() { var tx = new CommittableTransaction(); TransactionUtilities.DisplayTransactionInformation("TX created", tx.TransactionInformation); try { var s1 = new Student { FirstName = "Stephanie", LastName = "Nagel", Company = "CN innovation" }; var db = new StudentData(); await db.AddStudentAsync(s1, tx); if (TransactionUtilities.AbortTransaction()) { throw new ApplicationException("transaction abort"); } tx.Commit(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(); tx.Rollback(); } TransactionUtilities.DisplayTransactionInformation("TX completed", tx.TransactionInformation); }
static async Task TransactionScopeAsync() { using (var scope = new TransactionScope()) { Transaction.Current.TransactionCompleted += OnTransactionCompleted; TransactionUtilities.DisplayTransactionInformation("Ambient TX created", Transaction.Current.TransactionInformation); var s1 = new Student { FirstName = "Angela", LastName = "Nagel", Company = "Kantine M101" }; var db = new StudentData(); await db.AddStudentAsync(s1); if (!TransactionUtilities.AbortTransaction()) { scope.Complete(); } else { Console.WriteLine("transaction will be aborted"); } } // scope.Dispose() }
static void DependentTransaction() { var tx = new CommittableTransaction(); TransactionUtilities.DisplayTransactionInformation("Root TX created", tx.TransactionInformation); try { Task.Factory.StartNew(TxTask, tx.DependentClone(DependentCloneOption.BlockCommitUntilComplete)); if (TransactionUtilities.AbortTransaction()) { throw new ApplicationException("transaction abort"); } tx.Commit(); } catch (Exception ex) { Console.WriteLine(ex.Message); tx.Rollback(); } TransactionUtilities.DisplayTransactionInformation("TX finished", tx.TransactionInformation); }