static void Main() { var intVal = new Transactional <int>(1); var student = new Transactional <Student>(new Student()) { Value = { FirstName = "Andrew", LastName = "Wilson" } }; // Перед транзакцией Console.WriteLine("Before the transaction, value: {0}", intVal.Value); Console.WriteLine("Before the transaction, student: {0}", student.Value); using (var scope = new TransactionScope()) { intVal.Value = 2; // Внутри транзакции Console.WriteLine("Inside transaction, value: {0}", intVal.Value); student.Value.FirstName = "Ten"; student.Value.LastName = "SixtyNine"; if (!TxUtilities.AbortTransaction()) { scope.Complete(); } } // Вне транзакции Console.WriteLine("Outside of transaction, value: {0}", intVal.Value); Console.WriteLine("Outside of transaction, student: {0}", student.Value); }
public static void TaskMethod(object state) { try { var dependentTx = state as DependentTransaction; if (dependentTx == null) //Contract.Requires<ArgumentNullException>(dependentTx != null); { throw new ArgumentNullException("state"); } Transaction.Current = dependentTx; // NOTE: Передача зависимой транзакции в поток выполнения try { using (var scope = new TransactionScope()) { Transaction.Current.TransactionCompleted += OnTransactionCompleted; TxUtilities.DisplayTransactionInformation("Task TX", Transaction.Current.TransactionInformation); scope.Complete(); } } finally { dependentTx.Complete(); } } catch (TransactionAbortedException txAbortedEx) { Console.WriteLine("TaskMethod - Transaction was aborted, {0}", txAbortedEx); } }
public static void TransactionScopeSimple() { using (var scope = new TransactionScope()) { Transaction.Current.TransactionCompleted += Current_TransactionCompleted; TxUtilities.DisplayTransactionInformation("Ambient TX created", // Охватывающая транзакция создана Transaction.Current.TransactionInformation); var firstStudent = new Student { FirstName = "Angela", LastName = "Nagel", Company = "Kantine M101" }; var studentData = new StudentData(); studentData.AddStudent(firstStudent); if (!TxUtilities.AbortTransaction()) { scope.Complete(); } else { Console.WriteLine("Transaction will be aborted"); // Транзакция будет прекращена } } }
private static async Task CommittableTransactionAsync() { var committableTransaction = new CommittableTransaction(); TxUtilities.DisplayTransactionInformation( "TX created", committableTransaction.TransactionInformation); try { var student = new Student { FirstName = "Stephanie", LastName = "Nagel", Company = "CN innovation" }; var studentData = new StudentData(); await studentData.AddStudentAsync(student, committableTransaction); if (TxUtilities.AbortTransaction()) { throw new ApplicationException("Transaction abort"); // транзакция прервана } committableTransaction.Commit(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(); committableTransaction.Rollback(/*ex*/); } TxUtilities.DisplayTransactionInformation( "TX completed", committableTransaction.TransactionInformation); }
static void DependentTransaction() { var commitTx = new CommittableTransaction(); TxUtilities.DisplayTransactionInformation("Root TX Created", commitTx.TransactionInformation); try { // NOTE: Создаем зависимую транзакцию Task.Factory.StartNew(TxTask, commitTx.DependentClone(DependentCloneOption.BlockCommitUntilComplete)); if (TxUtilities.AbortTransaction()) { throw new ApplicationException("transaction abort"); } commitTx.Commit(); } catch (Exception ex) { Console.WriteLine(ex.Message); commitTx.Rollback(ex); } TxUtilities.DisplayTransactionInformation("TX finished", commitTx.TransactionInformation); }
static void TxTask(object state) { var depTx = state as DependentTransaction; if (depTx == null) { return; } TxUtilities.DisplayTransactionInformation("Dependent Transaction", depTx.TransactionInformation); Thread.Sleep(3000); depTx.Complete(); TxUtilities.DisplayTransactionInformation("Dependent Transaction Complete", depTx.TransactionInformation); }
public static void TaskMethod() { try { // NOTE: Здесь создается новая область действия в любом случае using (var scope = new TransactionScope(TransactionScopeOption.Required)) { Transaction.Current.TransactionCompleted += OnTransactionCompleted; TxUtilities.DisplayTransactionInformation("Thread TX", Transaction.Current.TransactionInformation); scope.Complete(); } } catch (TransactionAbortedException txAbortedEx) { Console.WriteLine("TaskMethod-Transaction was aborted {0}", txAbortedEx.Message); } }
static async void WriteFileSample() { using (var scope = new TransactionScope()) { FileStream stream = TransactedFile.GetTransactedFileStream("sample.txt"); var writer = new StreamWriter(stream); await writer.WriteLineAsync("Write a transactional file"); writer.Close(); if (!TxUtilities.AbortTransaction()) { scope.Complete(); } } }
public static void NewTransactionScope() { using (var scope = new TransactionScope()) { Transaction.Current.TransactionCompleted += Current_TransactionCompleted; TxUtilities.DisplayTransactionInformation("Ambient TX created", Transaction.Current.TransactionInformation); using (var innerScope = new TransactionScope(TransactionScopeOption.RequiresNew)) // Требуем новую независимую транзакцию { Transaction.Current.TransactionCompleted += Current_TransactionCompleted; TxUtilities.DisplayTransactionInformation("Inner transaction scope", Transaction.Current.TransactionInformation); innerScope.Complete(); } scope.Complete(); } }
static async void TransactionPromotion() { var commitTx = new CommittableTransaction(); // NOTE: Фиксируемая транзакция TxUtilities.DisplayTransactionInformation( "TX created", commitTx.TransactionInformation); try { var studentData = new StudentData(); var firstStudent = new Student { FirstName = "Matthias", LastName = "Nagel", Company = "CN innovation" }; await studentData.AddStudentAsync(firstStudent, commitTx); // NOTE: Присоединение локальной транзакции к соединению var secondStudent = new Student { FirstName = "Stephanie", LastName = "Nagel", Company = "CN innovation" }; await studentData.AddStudentAsync(secondStudent, commitTx); // NOTE: Транзакция продвигается до распределенной TxUtilities.DisplayTransactionInformation( "2nd connection enlisted", commitTx.TransactionInformation); if (TxUtilities.AbortTransaction()) { throw new ApplicationException("transaction abort"); } commitTx.Commit(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(); commitTx.Rollback(/*ex*/); } }
static void Main() { try { using (var scope = new TransactionScope()) { Transaction.Current.TransactionCompleted += OnTransactionCompleted; TxUtilities.DisplayTransactionInformation("Main thread TX", Transaction.Current.TransactionInformation); //Task.Factory.StartNew(TaskMethod); Task.Factory.StartNew(TaskMethod, Transaction.Current.DependentClone(DependentCloneOption.BlockCommitUntilComplete)); scope.Complete(); } } catch (TransactionAbortedException txAbortedEx) { Console.WriteLine("Main-Transaction was aborted {0}", txAbortedEx.Message); } Console.ReadKey(); }
private static void OnTransactionCompleted(object sender, TransactionEventArgs e) { TxUtilities.DisplayTransactionInformation("TX completed", e.Transaction.TransactionInformation); }