create table test (
id int identity,
id_int int default 1
)
insert test default values
go 1000
begin transaction
update test
set id_int = id_int
where id = 1000
waitfor delay '00:00:10'
commit
另一个会话无需等待即可得到结果:
set transaction isolation level read committed
select * from test
where id =1000
id | id_int
---------------
1000 |1
有人可以解释为什么我立即得到结果吗?
但是,当我使用可重复读隔离级别时,我应该等待:
set transaction isolation level repeatable read
select * from test
where id =1000
这是因为 SQL Server 正在进行非更新更新,请看下面它在两种情况下获取的锁,
情况1:(不更新更新)
它足够智能,可以在对象上处于活动状态
IS
时允许锁定。IX
案例2:(真实更新)
它在授予锁之前等待
IX
锁被释放IS
,以避免脏读。情况3:(具有可序列化隔离的非更新更新)
如果您想显式覆盖此行为,您可以
SERIALIZABLE
在执行非更新更新时使用隔离,这将获得一个X
锁,该锁会阻止其上的所有其他锁。