1.read uncommitted(脏读)(读取正在提交的数据)
read uncommitted(又称读取未提交内容)允许任务读取数据库中未提交的数据更改。
测试脚本:
创建表create table [dbo].[testtb](
[id] [int] null,
[name] [char](20) null
)
2.建立 事务a:插入数据
begin tran
insert into testtb values(5,'e')
waitfor delay '00:00:10'
commit;
3.建立事务b:读取数据
set transaction isolation level read uncommitted
begin tran
select * from testtb
commit;
4.运行事务a立即运行事务b,此时事务a还没有提交,而事务b可以读取事务a正在提交的数据
2.read committed(提交读)(不能读取正在提交的数据)
级别read committed(又称读取已提交内容)可防止脏读。该级别查询只读取已提交的数据更改。如果事务需要读取被另一未完成事务 修改的数据,该事务将等待,直到第一个事务完成(提交或回退)。
create table [dbo].[testtb](
[id] [int] null,
[name] [char](20) null
)
2.建立 事务a:插入数据
begin tran
insert into testtb values(5,'e')
waitfor delay '00:00:10'
commit;
3.建立事务b:读取数据
set transaction isolation level read committed
begin tran
select * from testtb
commit
4.运行事务a立即运行事务b,此时事务a还没有提交,而事务b必须等到事务a完成后才可以读取数据
3.repeatable read(可重复读)(读取数据是不能修改)
指定语句不能读取已由其他事务修改但尚未提交的行,并且指定,其他任何事务都不能在当前事务完成之前修改由当前事务读取的数据。
create table [dbo].[testtb](
[id] [int] null,
[name] [char](20) null
)
2.建立 事务a:更新数据
begin tran
update testtb set name='cv' where id='8'
waitfor delay '00:00:10'
commit;
3.建立事务b:读取数据
set transaction isolation level repeatable read
begin tran
select * from testtb
commit
4.运行事务a立即运行事务b,此时事务a还没有提交,而事务b必须等到事务a完成后才可以读取数据
4.serializable(顺序读)(读取数据是不可插入或修改)
语句不能读取已由其他事务修改但尚未提交的数据。任何其他事务都不能在当前事务完成之前修改由当前事务读取的数据。在当前事务完成之前,其他事务不能使用当前事务中任何语句读取的键值插入新行。create table [dbo].[testtb](
[id] [int] null,
[name] [char](20) null
)
2.建立 事务a:更新数据和插入数据
begin tran
insert into testtb values(9,'d')
update testtb set name='cv' where id='8'
waitfor delay '00:00:010'
commit;
3.建立事务b:读取数据
set transaction isolation level level serializable
begin tran
select * from testtb
commit;
4.运行事务a立即运行事务b,此时事务a还没有提交,而事务b必须等到事务a完成后才可以读取数据
