Exemple #1
0
        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);
        }
Exemple #2
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"); // Транзакция будет прекращена
         }
     }
 }
Exemple #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);
        }
Exemple #4
0
        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);
        }
Exemple #5
0
        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();
                }
            }
        }
Exemple #6
0
        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*/);
            }
        }