一、

 --该 read committed 默认事务隔离级别 在 systemuser修改事务未完成时   select * from  [SystemUser]  where id=62; 该语句是不可读取的

set transaction  isolation level   read committed;

begin transaction

update [SystemUser] set LoginId='test' where id=62;

 waitfor delay '00:00:10'

commit transaction;
go


--可新建查询窗口
begin transaction   readtran

 select * from  [SystemUser]  where id=62;
commit transaction;


二、 需手动设置  读提交快照处理打开状态 设置为true —— 右键数据——选项——读提交快照处理打开状态
在 allow_snapshot_isolation on    和 set transaction  isolation level   snapshot ;模式下事务未执行完是可以读取数据的
  -- 开启快照隔离
  --alter database [SimpleFactoring15] set allow_snapshot_isolation on
  -- 关闭快照隔离
 --alter database [SimpleFactoring15] set allow_snapshot_isolation off


  -- 开启快照隔离
 alter database [SimpleFactoring15] set allow_snapshot_isolation on
 set transaction  isolation level   snapshot ;

begin transaction

update [SystemUser] set LoginId='开启快照隔离' where id=62;

 waitfor delay '00:00:10'

commit transaction;
go


--可新建查询窗口
begin transaction   readtran

 select * from  [SystemUser]  where id=62;
commit transaction;

  

ead uncommitted | 0 未提交读
read committed | 1 已提交读
repeatable read | 2 可重复读
serializable | 3 可序列化
snapshot 快照(2005版本以后新加)

以下面的图为例,在事务A中会根据条件读取TABLE1,且读两次,事务B中会对同样的表TABLE1同样的条件进行UPDATE,在对表数据更新时会获得排他锁直至事务提交,

这样在事务A中如果设置不同的隔离级别就会有不同的效果。

1、隔离级别设置为read uncommitted,允许脏读,不管事务B何时Commit,事务A中的SELECT语句2都可以读出数据(有可能是脏数据,因事务B可能会ROLLBACK),且与语句1的数据不同。

2、隔离级别设置为read committed,不允许脏读,但允许“不可重复读”,即事务A中可以多次读,不管事务B中是否Commit,如果SELECT语句3执行时事务B还没有Commit,读取结果与SELECT 语句1相同,反之则不同。

3、隔离级别设置为repeatable read,不允许脏读,也不允许“不可重复读”,但允许”幻读“,示例中,事务A中的SELECT语句3的执行一定要等到事务B Commit之后才能执行成功。

4、隔离级别设置为serializable,这是最高级别的隔离,串行化读,事务只能一个一个执行,避免了脏读、不可重复读、幻读。

5、隔离级别设置为snapshot ,读取数据时,可以保证读操作读取的行是事务开始时可用的最后提交版本。这意味着这种隔离级别可以保证读取的是已经提交过的数据,并且可以实现可重复读,也能确保不会幻读。

02-13 19:28