并发可能产生的三种问题

脏读

定义:A事务执行过程中B事务读取了A事务的修改,但是A事务并没有结束(提交),A事务后来可能成功也可能失败。

比喻:A修改了源代码并且并没有提交到源代码系统,A直接通过QQ将代码发给了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))
             {
                 //添加数据
                 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审查过的代码,而这部分代码可能不符合规范了。

代码示例
复制代码 代码如下:
[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会感觉自己的脑袋有点头疼。

代码示例
复制代码 代码如下:
[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());
                 }
             }
         }

四种隔离级别如何处理并发问题
  脏读 不可重复读 幻读 读未提交 允许 允许 允许 读已提交 不允许 允许 允许 可重复读 不允许 不允许 允许 串行化 不允许 不允许 不允许

标签:
脏读,不可重复读,幻读

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。