Heim  >  Artikel  >  Datenbank  >  数据库事务的级别

数据库事务的级别

WBOY
WBOYOriginal
2016-06-07 15:41:451343Durchsuche

1.read uncommitted(脏读)(读取正在提交的数据) read uncommitted(又称读取未提交内容)允许任务读取数据库中未提交的数据更改。 测试脚本: 创建表 CREATE TABLE [dbo].[testTb]( [ID] [int] NULL, [Name] [char](20) NULL ) 2.建立 事务A:插入数据 begin

1.read uncommitted(脏读)(读取正在提交的数据)

   read uncommitted(又称读取未提交内容)允许任务读取数据库中未提交的数据更改。

 

 测试脚本:

  1.   创建表

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完成后才可以读取数据

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn