国产探花免费观看_亚洲丰满少妇自慰呻吟_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捆綁壓縮技術,有需要的朋友可以參考一下

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 滕州市| 延边| 安龙县| 华阴市| 富顺县| 奉新县| 虹口区| 安泽县| 文化| 营山县| 台北市| 开化县| 大邑县| 文安县| 敦化市| 平顺县| 丰原市| 西和县| 南木林县| 鸡泽县| 陕西省| 福贡县| 余干县| 库尔勒市| 湄潭县| 淮安市| 通海县| 道真| 盐城市| 宝应县| 新干县| 正阳县| 竹山县| 常山县| 新疆| 泸定县| 旬阳县| 古交市| 濮阳县| 特克斯县| 都兰县|