static void TransactionScope() { using (var scope = new TransactionScope()) { Transaction.Current.TransactionCompleted += OnTransactionCompleted; Utilities.DisplayTransactionInformation("Ambient TX created", Transaction.Current.TransactionInformation); var s1 = new Student { FirstName = "Ingo", LastName = "Rammer", Company = "thinktecture" }; var db = new StudentData(); db.AddStudent(s1); if (!Utilities.AbortTx()) { scope.Complete(); } else { Console.WriteLine("transaction will be aborted"); } } // scope.Dispose() }
static void CommittableTransaction() { var tx = new CommittableTransaction(); Utilities.DisplayTransactionInformation("TX created", tx.TransactionInformation); try { var s1 = new Student { FirstName = "Neno", LastName = "Loye", Company = "thinktecture" }; var db = new StudentData(); db.AddStudent(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 void TransactionPromotion() { var tx = new CommittableTransaction(); Utilities.DisplayTransactionInformation("TX created", tx.TransactionInformation); try { var s1 = new Student { FirstName = "Jörg", LastName = "Neumann", Company = "thinktecture" }; var db = new StudentData(); db.AddStudent(s1, tx); Student s2 = new Student { FirstName = "Richard", LastName = "Blewett", Company = "thintkecture" }; db.AddStudent(s2, tx); Utilities.DisplayTransactionInformation("2nd connection enlisted", tx.TransactionInformation); if (Utilities.AbortTx()) { throw new ApplicationException("transaction abort"); } tx.Commit(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(); tx.Rollback(); } Utilities.DisplayTransactionInformation("TX finished", tx.TransactionInformation); }
static void Main() { CommittableTransaction tx = new CommittableTransaction(); Utilities.DisplayTransactionInformation("TX created", tx.TransactionInformation); try { Student s1 = new Student(); s1.FirstName = "Neno"; s1.LastName = "Loye"; s1.Company = "thinktecture"; StudentData db = new StudentData(); db.AddStudent(s1, tx); Student s2 = new Student(); s2.FirstName = "Dominick"; s2.LastName = "Baier"; s2.Company = "thinktecture"; db.AddStudent(s2, tx); Utilities.DisplayTransactionInformation("2nd connection enlisted", tx.TransactionInformation); 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); }