.NET中 關(guān)于臟讀 不可重復(fù)讀與幻讀的代碼示例
并發(fā)可能產(chǎn)生的三種問題
臟讀
定義:A事務(wù)執(zhí)行過程中B事務(wù)讀取了A事務(wù)的修改,但是A事務(wù)并沒有結(jié)束(提交),A事務(wù)后來可能成功也可能失敗。
比喻:A修改了源代碼并且并沒有提交到源代碼系統(tǒng),A直接通過QQ將代碼發(fā)給了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))
{
//添加數(shù)據(jù)
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());
}
}
不可重復(fù)讀
定義:A事務(wù)讀取了兩次數(shù)據(jù),在這兩次的讀取過程中B事務(wù)修改了數(shù)據(jù),A事務(wù)的這兩次讀取出來的數(shù)據(jù)不一樣了(不可重復(fù)讀)。
比喻:A在做源代碼審查,在審查的過程中獲取了兩次源代碼,在這兩次獲取期間B修改了源代碼,B修改的很可能是A審查過的代碼,而這部分代碼可能不符合規(guī)范了。
代碼示例
[TestMethod]
public void 不可重復(fù)讀_測試()
{
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))
{
//修改數(shù)據(jù)
using (var context = new TestEntities())
{
context.Tables.First().Name = "段光偉";
context.SaveChanges();
}
ts2.Complete();
}
autoResetEvent.Set();
});
autoResetEvent.WaitOne();
//不可重復(fù)讀測試
using (var context = new TestEntities())
{
Assert.AreEqual("段光偉", context.Tables.First().Name);
}
}
}
幻讀
定義:A事務(wù)讀取了兩次數(shù)據(jù),在這兩次的讀取過程中B事務(wù)添加了數(shù)據(jù),A事務(wù)的這兩次讀取出來的集合不一樣了(幻讀)。
比喻:A在統(tǒng)計文件數(shù)據(jù),為了統(tǒng)計精確A統(tǒng)計了兩次,在這兩次的統(tǒng)計過程中B添加了一個文件,A發(fā)現(xiàn)這兩次統(tǒng)計的數(shù)量不一樣(幻讀),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))
{
//添加數(shù)據(jù)
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());
}
}
}
四種隔離級別如何處理并發(fā)問題
臟讀 | 不可重復(fù)讀 | 幻讀 | |
讀未提交 | 允許 | 允許 | 允許 |
讀已提交 | 不允許 | 允許 | 允許 |
可重復(fù)讀 | 不允許 | 不允許 | 允許 |
串行化 | 不允許 | 不允許 | 不允許 |
相關(guān)文章
.NET AppSettings與ConnectionStrings使用案例詳解
這篇文章主要介紹了.NET AppSettings與ConnectionStrings使用案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08.Net Core簡單使用Mvc內(nèi)置的Ioc(續(xù))
怎樣直接獲取Ioc中的實例對象,而不是以構(gòu)造函數(shù)的方式進行獲取呢?這篇文章繼續(xù)為大家介紹.Net Core簡單使用Mvc內(nèi)置的Ioc2018-03-03asp.net c#采集需要登錄頁面的實現(xiàn)原理及代碼
當我們采集頁面的時候,如果被采集的網(wǎng)站需要登錄才能采集,原理搞清楚了,就好辦了,我們所要做的僅僅是在采集的時候(或者說HttpWebRequest提交數(shù)據(jù)的時候),將Cookie信息放入Http請求頭里面就可以了,感興趣的朋友可以了解下,或許對你有所幫助2013-02-02.NET實現(xiàn)熱插拔功能(動態(tài)替換功用)方案實例
如果某個"功能"需要動態(tài)更新?這種動態(tài)更新,可能是需求驅(qū)動的,也可能是為了修改 BUG,面對這種場景,如何實現(xiàn)“熱插拔”呢?先解釋一下“熱插拔”:在系統(tǒng)運行過程動態(tài)替換某些功能,不用重啟系統(tǒng)進程。下面看例子2013-11-11