コード例 #1
0
        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()
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        static void TxTask(object obj)
        {
            var tx = obj as DependentTransaction;

            if (tx == null)
            {
                return;
            }

            TransactionUtilities.DisplayTransactionInformation("Dependent Transaction",
                                                               tx.TransactionInformation);

            Thread.Sleep(3000);

            tx.Complete();

            TransactionUtilities.DisplayTransactionInformation("Dependent TX Complete",
                                                               tx.TransactionInformation);
        }
コード例 #5
0
        static void NestedScopes()
        {
            using (var scope = new TransactionScope())
            {
                Transaction.Current.TransactionCompleted += OnTransactionCompleted;

                TransactionUtilities.DisplayTransactionInformation("Ambient TX created",
                                                                   Transaction.Current.TransactionInformation);

                using (var scope2 =
                           new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    Transaction.Current.TransactionCompleted += OnTransactionCompleted;

                    TransactionUtilities.DisplayTransactionInformation(
                        "Inner Transaction Scope",
                        Transaction.Current.TransactionInformation);

                    scope2.Complete();
                }
                scope.Complete();
            }
        }
コード例 #6
0
 static void OnTransactionCompleted(object sender, TransactionEventArgs e)
 {
     TransactionUtilities.DisplayTransactionInformation("TX completed",
                                                        e.Transaction.TransactionInformation);
 }