国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

.NET中 關于臟讀 不可重復讀與幻讀的代碼示例_.Net教程

2024-07-10 12:52:16
字體:
來源:轉載
供稿:網友

推薦:關于.NET Framework中的設計模式--應用策略模式為List排序
本篇文章,小編將為大家介紹關于.NET Framework中的設計模式--應用策略模式為List排序,有需要的朋友可以參考一下

并發可能產生的三種問題

臟讀

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

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

代碼示例

復制代碼 代碼如下:www.CuoXIn.com

[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審查過的代碼,而這部分代碼可能不符合規范了。

代碼示例

復制代碼 代碼如下:www.CuoXIn.com

[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會感覺自己的腦袋有點頭疼。

代碼示例

復制代碼 代碼如下:www.CuoXIn.com

[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());
}
}
}

四種隔離級別如何處理并發問題

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 堆龙德庆县| 弥勒县| 磐石市| 日土县| 上高县| 陕西省| 玉门市| 鹿邑县| 榕江县| 顺平县| 邯郸市| 富宁县| 马龙县| 常德市| 柳州市| 盐津县| 扎鲁特旗| 积石山| 若尔盖县| 松江区| 叙永县| 微山县| 旅游| 峡江县| 正宁县| 嘉黎县| 九龙城区| 磴口县| 松溪县| 日照市| 桂阳县| 临清市| 海城市| 兴海县| 兴国县| 砀山县| 辉南县| 托里县| 闻喜县| 肇源县| 呼和浩特市|