并发处理-隔离级别

READ UNCOMMITTED:指定语句可以读取已由其他事务修改但尚未提交的行。

同时执行以下sql语句:

A.--隔离级别-read committed
set transaction isolation level READ COMMITTED
Begin Tran

Update Sys_Items set ItemName='Changed123' where Id=2
WAITFOR DELAY '000:00:15'
commit tran
B.
set
transaction isolation level READ UNCOMMITTED select * from Sys_Items where Id=2

B因为是【READ UNCOMMITTED】,所以A还未提交的时候。便已经可以查询出修改后的结果了。

READ COMMITTED:指定语句不能读取已由其他事务修改但尚未提交的数据。

实例1:

 1 --隔离级别-read committed
 2 set transaction isolation level read committed
 3 Begin Tran
 4 
 5 Update Sys_Items set ItemName='Changed' where Id=2
 6 WAITFOR DELAY '000:00:20'
 7 rollback tran
 8 
 9 set transaction isolation level read committed
10 select * from Sys_Items where Id=2
11 
12 --隔离级别-repeatable read
13 set transaction isolation level repeatable read
14 Begin Tran
15 Update Sys_Items set ItemName='Changed' where Id=2
16 WAITFOR DELAY '000:00:20'
17 rollback tran
View Code

实例2:

若同时执行上述两个语句,则select查询必须等待update执行完毕才能执行即要等待30秒

在第一个连接中执行以下语句

begin tran
update table1
set A='aa'
where B='b2'
waitfor delay '00:00:30' --等待30秒
commit tran

在第二个连接中执行以下语句

begin tran
select * from table1
where B='b2'
commit tran
作者:chenze
出处:https://www.cnblogs.com/chenze-Index/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出。以免更多的人被误导。
原文地址:https://www.cnblogs.com/chenze-Index/p/9673521.html