Home Database Mysql Tutorial SqlServer事务中的可重复读和序列化隔离界别

SqlServer事务中的可重复读和序列化隔离界别

Jun 07, 2016 pm 03:44 PM
sqlserver affairs Serialization boundary isolation

有表t_lock: ID是主键,表中有5行数据,1~2,4~6 可重复读: SET TRANSACTION ISOLATION LEVEL REPEATABLE READ begin tran select * from t_lock where id between 1 and 6 执行这个查询后,会在表ID为1,2,4,5,6的行上加上共享锁(s) 执行插入语句 insert

有表t_lock:

SqlServer事务中的可重复读和序列化隔离界别

ID是主键,表中有5行数据,1~2,4~6

 

可重复读:

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ 
begin tran
select * from t_lock where id between 1 and 6

执行这个查询后,会在表ID为1,2,4,5,6的行上加上共享锁(s)

执行插入语句

insert into t_lock values(3,'3')

执行成功,原因是插入的是ID为3的数据行并加上排他锁(x),插入语句不会去1,2,4,5,6行申请排它锁,否则就发生阻塞了

 

 

序列化:

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
begin tran
select * from t_lock where id between 1 and 6

 

根据序列化级别的定义执行查询后会在ID为1~6的范围上RangeS-S范围共享锁,虽然ID为3的行在数据库并不存在,但是由于这里在ID为1~6加上的是范围锁,所以实际上ID为3的行也被加上了RangeS-S锁,一定要记住范围锁是连续的范围,并不因为数据库里数据不存在就不加锁,比如你可以同样执行插入ID为3的sql语句:

insert into t_lock values(3,'3')

 

结果发生了阻塞,原因就是ID为1~6的行(不管是否存在)都被加上了范围为共享锁,在这个范围除了查询什么也做不了,而上面的插入语句要对ID为3的数据行申请排它锁,肯定会被阻塞.

 

 

序列化的小陷阱:

按道理来说执行了以下sql:

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
begin tran
select * from t_lock where id between 1 and 6

 

然后插入ID大于6和ID小于1的数据行应该不会被阻塞,因为插入数据的ID位于1~6的范围之外了,可是你可以试着执行以下插入语句:

insert into t_lock values(9,'9')
insert into t_lock values(-1,'-1')

无论是插入ID为-1的行还是ID为9的行都被阻塞,这是为什么?明明插入数据的ID在范围之外啊?

 

让我们更改数据库中的数据,再来看看两种情况就明白了

首先如果在事务中使用序列化查询后,数据库里的数据是这样:

SqlServer事务中的可重复读和序列化隔离界别

执行insert into t_lock values(9,'9')不会阻塞
但是执行insert into t_lock values(-1,'-1')会被阻塞

 

如果在事务中使用序列化查询后,数据库里的数据是这样:

SqlServer事务中的可重复读和序列化隔离界别

执行insert  into t_lock values(-1,'-1')不会阻塞
但是执行insert into t_lock values(9,'9')会被阻塞

 

如果在事务中使用序列化查询后,数据库里的数据是这样:

SqlServer事务中的可重复读和序列化隔离界别

执行insert  into t_lock values(-1,'-1')或insert into t_lock values(9,'9')都不会被阻塞

 

看出点眉目来了吗?
原来SqlServer在对行加范围锁的时候会先去探测数据库中加锁范围外是否还有其他数据行,如果没有,会对加锁范围外的其它行也加上范围锁,比如本文中范围锁会对ID为1~6的数据行加范围锁,它会先去查看数据库中是否存在ID小于1的数据行,如果不存在它会将ID小于1和ID为1~6的行全加上范围锁,同样它也会去看数据库中是否有ID大于6的数据行,如果没有它会将ID大于6和ID为1~6的行全加上范围锁

  • 所以在最后一个图中因为SqlServer发现数据库中既有ID小于1的数据行(ID=0)也有ID大于6的数据行(ID=7),所以只在ID为1~6的范围上加范围锁,所以执行insert  into t_lock values(-1,'-1')或insert into t_lock values(9,'9')都不会被阻塞
  • 倒数第二个图的情况是SqlServer在数据库中只发现了有小于1的数据行(ID=0),所以将ID大于1的行都加上了范围锁,执行insert  into t_lock values(9,'9')被阻塞
  • 倒数第三个图的情况是SqlServer在数据库中只发现了有大于6的数据行(ID=7),所以将ID小于6的行都加上了范围锁,执行insert  into t_lock values(-1,'-1')被阻塞

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
How to import mdf file into sqlserver How to import mdf file into sqlserver Apr 08, 2024 am 11:41 AM

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

How to solve the problem that the object named already exists in the sqlserver database How to solve the problem that the object named already exists in the sqlserver database Apr 05, 2024 pm 09:42 PM

For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

How to recover accidentally deleted database in sqlserver How to recover accidentally deleted database in sqlserver Apr 05, 2024 pm 10:39 PM

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.

How to check sqlserver port number How to check sqlserver port number Apr 05, 2024 pm 09:57 PM

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

What to do if the sqlserver service cannot be started What to do if the sqlserver service cannot be started Apr 05, 2024 pm 10:00 PM

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

Where is the sqlserver database? Where is the sqlserver database? Apr 05, 2024 pm 08:21 PM

SQL Server database files are usually stored in the following default location: Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data The database file location can be customized by modifying the database file path setting.

How to delete sqlserver if the installation fails? How to delete sqlserver if the installation fails? Apr 05, 2024 pm 11:27 PM

If the SQL Server installation fails, you can clean it up by following these steps: Uninstall SQL Server Delete registry keys Delete files and folders Restart the computer

How to change sqlserver English installation to Chinese How to change sqlserver English installation to Chinese Apr 05, 2024 pm 10:21 PM

SQL Server English installation can be changed to Chinese by following the following steps: download the corresponding language pack; stop the SQL Server service; install the language pack; change the instance language; change the user interface language; restart the application.

See all articles