并發(fā)可能產(chǎn)生的三種問(wèn)題
臟讀
定義:A事務(wù)執(zhí)行過(guò)程中B事務(wù)讀取了A事務(wù)的修改,但是A事務(wù)并沒(méi)有結(jié)束(提交),A事務(wù)后來(lái)可能成功也可能失敗。
比喻:A修改了源代碼并且并沒(méi)有提交到源代碼系統(tǒng),A直接通過(guò)QQ將代碼發(fā)給了B,A后來(lái)取消了修改。
代碼示例
復(fù)制代碼 代碼如下:
[TestMethod]
public void 臟讀_測(cè)試()
{
//前置條件
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))
{
//臟讀測(cè)試
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ù),在這兩次的讀取過(guò)程中B事務(wù)修改了數(shù)據(jù),A事務(wù)的這兩次讀取出來(lái)的數(shù)據(jù)不一樣了(不可重復(fù)讀)。
比喻:A在做源代碼審查,在審查的過(guò)程中獲取了兩次源代碼,在這兩次獲取期間B修改了源代碼,B修改的很可能是A審查過(guò)的代碼,而這部分代碼可能不符合規(guī)范了。
代碼示例
復(fù)制代碼 代碼如下:
[TestMethod]
public void 不可重復(fù)讀_測(cè)試()
{
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ù)讀測(cè)試
using (var context = new TestEntities())
{
Assert.AreEqual("段光偉", context.Tables.First().Name);
}
}
}
幻讀
定義:A事務(wù)讀取了兩次數(shù)據(jù),在這兩次的讀取過(guò)程中B事務(wù)添加了數(shù)據(jù),A事務(wù)的這兩次讀取出來(lái)的集合不一樣了(幻讀)。
比喻:A在統(tǒng)計(jì)文件數(shù)據(jù),為了統(tǒng)計(jì)精確A統(tǒng)計(jì)了兩次,在這兩次的統(tǒng)計(jì)過(guò)程中B添加了一個(gè)文件,A發(fā)現(xiàn)這兩次統(tǒng)計(jì)的數(shù)量不一樣(幻讀),A會(huì)感覺(jué)自己的腦袋有點(diǎn)頭疼。
代碼示例
復(fù)制代碼 代碼如下:
[TestMethod]
public void 幻讀_測(cè)試()
{
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();
//幻讀測(cè)試
using (var context = new TestEntities())
{
Assert.AreEqual(2, context.Tables.Count());
}
}
}
四種隔離級(jí)別如何處理并發(fā)問(wèn)題
|
臟讀 |
不可重復(fù)讀 |
幻讀 |
讀未提交 |
允許 |
允許 |
允許 |
讀已提交 |
不允許 |
允許 |
允許 |
可重復(fù)讀 |
不允許 |
不允許 |
允許 |
串行化 |
不允許 |
不允許 |
不允許 |