欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.NET中 关于脏读 不可重复读与幻读的代码示例

程序员文章站 2024-03-02 10:50:40
并发可能产生的三种问题 脏读 定义:a事务执行过程中b事务读取了a事务的修改,但是a事务并没有结束(提交),a事务后来可能成功也可能失败。 比喻:a修改了源代码并且并...

并发可能产生的三种问题

脏读

定义: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());
                 }
             }
         }

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