程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> .NET中 關於髒讀 不可重復讀與幻讀的代碼示例

.NET中 關於髒讀 不可重復讀與幻讀的代碼示例

編輯:ASP.NET基礎

並發可能產生的三種問題

髒讀

定義:A事務執行過程中B事務讀取了A事務的修改,但是A事務並沒有結束(提交),A事務後來可能成功也可能失敗。

比喻:A修改了源代碼並且並沒有提交到源代碼系統,A直接通過QQ將代碼發給了B,A後來取消了修改。

代碼示例
復制代碼 代碼如下:
[TestMethod]
         public void 髒讀_測試()
         {
             //前置條件
             using (var context = new TestEntities())
             {
                 Assert.AreEqual(1, context.Tables.Count());
             }

             var autoResetEvent = new AutoResetEvent(false);

             var transactionOptions1 = new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted };
             var transactionOptions2 = new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted };

             using (var ts1 = new TransactionScope(TransactionScopeOption.Required, transactionOptions1))
             {
                 //添加數據
                 using (var context = new TestEntities())
                 {
                     context.Tables.Add(new Table() { Id = Guid.NewGuid(), Name = "段光偉" });
                     context.SaveChanges();
                 }

                 ThreadPool.QueueUserWorkItem(data =>
                 {
                     using (var ts2 = new TransactionScope(TransactionScopeOption.Required, transactionOptions2))
                     {
                         //髒讀測試
                         using (var context = new TestEntities())
                         {
                             Assert.AreEqual(2, context.Tables.Count());
                         }
                     }

                     autoResetEvent.Set();
                 });

                 autoResetEvent.WaitOne();
             }

             //前置條件
             using (var context = new TestEntities())
             {
                 Assert.AreEqual(1, context.Tables.Count());
             }
         }

不可重復讀

定義:A事務讀取了兩次數據,在這兩次的讀取過程中B事務修改了數據,A事務的這兩次讀取出來的數據不一樣了(不可重復讀)。

比喻:A在做源代碼審查,在審查的過程中獲取了兩次源代碼,在這兩次獲取期間B修改了源代碼,B修改的很可能是A審查過的代碼,而這部分代碼可能不符合規范了。

代碼示例
復制代碼 代碼如下:
[TestMethod]
         public void 不可重復讀_測試()
         {
             var autoResetEvent = new AutoResetEvent(false);

             var transactionOptions1 = new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted };
             var transactionOptions2 = new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted };

             using (var ts1 = new TransactionScope(TransactionScopeOption.Required, transactionOptions1))
             {
                 //前置條件
                 using (var context = new TestEntities())
                 {
                     Assert.AreEqual("李妞妞", context.Tables.First().Name);
                 }

                 ThreadPool.QueueUserWorkItem(data =>
                 {
                     using (var ts2 = new TransactionScope(TransactionScopeOption.Required, transactionOptions2))
                     {
                         //修改數據
                         using (var context = new TestEntities())
                         {
                             context.Tables.First().Name = "段光偉";
                             context.SaveChanges();
                         }

                         ts2.Complete();   
                     }

                     autoResetEvent.Set();
                 });

                 autoResetEvent.WaitOne();

                 //不可重復讀測試
                 using (var context = new TestEntities())
                 {
                     Assert.AreEqual("段光偉", context.Tables.First().Name);
                 }
             }
         }

幻讀

定義:A事務讀取了兩次數據,在這兩次的讀取過程中B事務添加了數據,A事務的這兩次讀取出來的集合不一樣了(幻讀)。

比喻:A在統計文件數據,為了統計精確A統計了兩次,在這兩次的統計過程中B添加了一個文件,A發現這兩次統計的數量不一樣(幻讀),A會感覺自己的腦袋有點頭疼。

代碼示例
復制代碼 代碼如下:
[TestMethod]
         public void 幻讀_測試()
         {
             var autoResetEvent = new AutoResetEvent(false);

             var transactionOptions1 = new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead };
             var transactionOptions2 = new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted };

             using (var ts1 = new TransactionScope(TransactionScopeOption.Required, transactionOptions1))
             {
                 //前置條件
                 using (var context = new TestEntities())
                 {
                     Assert.AreEqual(1, context.Tables.Count());
                 }

                 ThreadPool.QueueUserWorkItem(data =>
                 {
                     using (var ts2 = new TransactionScope(TransactionScopeOption.Required, transactionOptions2))
                     {
                         //添加數據
                         using (var context = new TestEntities())
                         {
                             context.Tables.Add(new Table() { Id = Guid.NewGuid(), Name = "段光偉" });
                             context.SaveChanges();
                         }

                         ts2.Complete();
                     }

                     autoResetEvent.Set();
                 });

                 autoResetEvent.WaitOne();

                 //幻讀測試
                 using (var context = new TestEntities())
                 {
                     Assert.AreEqual(2, context.Tables.Count());
                 }
             }
         }

四種隔離級別如何處理並發問題
  髒讀 不可重復讀 幻讀 讀未提交 允許 允許 允許 讀已提交 不允許 允許 允許 可重復讀 不允許 不允許 允許 串行化 不允許 不允許 不允許

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved