Пример #1
0
        /// <summary>
        /// Have T1 and T2 read R1, both shall get the result immediately.
        /// Have T1, T2 both write R2 concurrently, The result shall be correct. Demo locks and two transaction runs concurrently.
        /// </summary>
        public void ConcurrentTransactions()
        {
            StartUp();
            Transaction t1 = WorkflowControl.Start();
            Transaction t2 = WorkflowControl.Start();

            Assert.AreEqual(WorkflowControl.QueryCar(t1, "Vegas"), WorkflowControl.QueryCar(t2, "Vegas"));
            AutoResetEvent sync1 = new AutoResetEvent(false);
            AutoResetEvent sync2 = new AutoResetEvent(false);

            ThreadPool.QueueUserWorkItem(o =>
            {
                WorkflowControl.AddCars(t1, "Seattle", 1, 3);
                WorkflowControl.Commit(t1);
                sync1.Set();
            });
            ThreadPool.QueueUserWorkItem(o =>
            {
                WorkflowControl.AddCars(t2, "Seattle", 2, 3);
                WorkflowControl.Commit(t2);
                sync2.Set();
            });
            // here is just to make sure t2 commits before t1, so the price will be 2.
            sync1.WaitOne();
            sync2.WaitOne();

            string actual = PrintCars();

            Assert.AreEqual("Vegas,1,3;NewYork,10,30;Seattle,3,3;", actual);
        }