static async Task CommittableTransactionAsync() { var tx = new CommittableTransaction(); Utilities.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 (Utilities.AbortTx()) { throw new ApplicationException("transaction abort"); } tx.Commit(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(); tx.Rollback(); } Utilities.DisplayTransactionInformation("TX completed", tx.TransactionInformation); }
static async Task TransactionScopeAsync() { using (var scope = new TransactionScope()) { Transaction.Current.TransactionCompleted += OnTransactionCompleted; Utilities.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 (!Utilities.AbortTx()) { scope.Complete(); } else { Console.WriteLine("transaction will be aborted"); } } // scope.Dispose() }
static async Task TransactionPromotionAsync() { var tx = new CommittableTransaction(); TransactionUtilities.DisplayTransactionInformation("TX created", tx.TransactionInformation); try { var s1 = new Student { FirstName = "Matthias", LastName = "Nagel", Company = "CN innovation" }; var db = new StudentData(); await db.AddStudentAsync(s1, tx); var s2 = new Student { FirstName = "Stephanie", LastName = "Nagel", Company = "CN innovation" }; await db.AddStudentAsync(s2, tx); TransactionUtilities.DisplayTransactionInformation("2nd connection enlisted", tx.TransactionInformation); if (TransactionUtilities.AbortTransaction()) { throw new ApplicationException("transaction abort"); } tx.Commit(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(); tx.Rollback(); } TransactionUtilities.DisplayTransactionInformation("TX finished", tx.TransactionInformation); }