日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

.NET中 關(guān)于臟讀 不可重復(fù)讀與幻讀的代碼示例_.Net教程

編輯Tag賺U幣

推薦:關(guān)于.NET Framework中的設(shè)計(jì)模式--應(yīng)用策略模式為L(zhǎng)ist排序
本篇文章,小編將為大家介紹關(guān)于.NET Framework中的設(shè)計(jì)模式--應(yīng)用策略模式為L(zhǎng)ist排序,有需要的朋友可以參考一下

并發(fā)可能產(chǎn)生的三種問題

臟讀

定義:A事務(wù)執(zhí)行過程中B事務(wù)讀取了A事務(wù)的修改,但是A事務(wù)并沒有結(jié)束(提交),A事務(wù)后來(lái)可能成功也可能失敗。

比喻:A修改了源代碼并且并沒有提交到源代碼系統(tǒng),A直接通過QQ將代碼發(fā)給了B,A后來(lái)取消了修改。

代碼示例

復(fù)制代碼 代碼如下:www.hl5o.cn

[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ù),在這兩次的讀取過程中B事務(wù)修改了數(shù)據(jù),A事務(wù)的這兩次讀取出來(lái)的數(shù)據(jù)不一樣了(不可重復(fù)讀)。

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

代碼示例

復(fù)制代碼 代碼如下:www.hl5o.cn

[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ù),在這兩次的讀取過程中B事務(wù)添加了數(shù)據(jù),A事務(wù)的這兩次讀取出來(lái)的集合不一樣了(幻讀)。

比喻:A在統(tǒng)計(jì)文件數(shù)據(jù),為了統(tǒng)計(jì)精確A統(tǒng)計(jì)了兩次,在這兩次的統(tǒng)計(jì)過程中B添加了一個(gè)文件,A發(fā)現(xiàn)這兩次統(tǒng)計(jì)的數(shù)量不一樣(幻讀),A會(huì)感覺自己的腦袋有點(diǎn)頭疼。

代碼示例

復(fù)制代碼 代碼如下:www.hl5o.cn

[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ā)問題

分享:基于Asp.Net MVC4 Bundle捆綁壓縮技術(shù)的介紹
本篇文章,小編將為大家介紹,Asp.Net MVC4 Bundle捆綁壓縮技術(shù),有需要的朋友可以參考一下

來(lái)源:模板無(wú)憂//所屬分類:.Net教程/更新時(shí)間:2013-04-22
相關(guān).Net教程