コード例 #1
0
 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"); // Транзакция будет прекращена
         }
     }
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: simple555a/DotNetAppDev
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: simple555a/DotNetAppDev
        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);
        }