Home Database Mysql Tutorial SQL SERVER的锁机制(三)概述(锁与事务隔离级别)

SQL SERVER的锁机制(三)概述(锁与事务隔离级别)

Jun 07, 2016 pm 05:44 PM
server affairs mechanism Overview level isolation

接上文SQL SERVER的锁机制(二)概述(锁的兼容性与可以锁定的资源) 五、锁与事务隔离级别 事务隔离级别简单的说,就是当激活事务时,控制事务内因 SQL 语句产生的锁定需要保留多入,影响范围多大,以防止多人访问时,在事务内发生数据查询的错误。设置事务

接上文SQL SERVER的锁机制(二)——概述(锁的兼容性与可以锁定的资源)

 

五、锁与事务隔离级别

事务隔离级别简单的说,香港服务器,就是当激活事务时,控制事务内因SQL语句产生的锁定需要保留多入,影响范围多大,以防止多人访问时,在事务内发生数据查询的错误。设置事务隔离级别将影响整条连接。

SQL Server 数据库引擎支持所有这些隔离级别:

· 未提交读(隔离事务的最低级别,只能保证不读取物理上损坏的数据)

· 已提交读(数据库引擎的默认级别)

· 可重复读

· 可序列化(隔离事务的最高级别,事务之间完全隔离)

SQL Server 还支持使用行版本控制的两个事务隔离级别。一个是已提交读隔离的新实现,另一个是新事务隔离级别(快照)。

 

设置语句如下:

SET TRANSACTION ISOLATION LEVEL

    { READ UNCOMMITTED

    | READ COMMITTED

    | REPEATABLE READ

    | SNAPSHOT

    | SERIALIZABLE

    }

[ ; ]

 

(一)未提交读

未提交读是最低的事务隔离级别,允许读取其他事务已经修改但未提交的数据行。SQL SERVER 当此事务等级进行尝试读取数据时,不会放置共享锁,直接读取数据,所以忽略已存在的互斥锁。换句话说,即使该资源已经受到了独占锁的保护,当使用未提交读隔离级别时,此数据还是可以被读取,加快查询速度,但是会读取到别人未修改的数据,所以此种读取被称为脏读。此种隔离级别适合不在乎数据变更的查询场景。此隔离级别与SELECT 语句搭配 NOLOCK 所起到的效果相同

未提交读示例:

--1.--1.创建测试表

create table tbUnRead

(ID INT,

name nvarchar(20)

)

--2新增记录

insert tbUnRead 

select 1,'Tom'

union

select 2,'Jack'

--3开启事务,并进行更新

 

begin tran

update tbUnRead

set name='Jack_upd'

where ID=2

SELECT @@TRANCOUNT

事务查询结果如下:

 

set Transaction isolation level read uncommitted

--6查询数据,查询到的数据是修改之后的数据。

select * from tbUnRead where ID=2

如下图:

 

 

(二)已提交读

 

控制的隔离下读取数据。读取操作不会获取正被读取的数据上的共享锁(锁),因此不会阻塞正在修改数据的事务。同时,香港服务器,由于减少了所获取的锁的数量,因此最大程度地降低了锁定资源的开销。使用行版本控制的已提交读隔离和快照隔离旨在提供副本数据的语句级或事务级读取一致性。

示例一:设置READ_COMMITTED_SNAPSHOT为OFF

--1.创建测试表

create table tbUnRead

(ID INT,

name nvarchar(20)

)

--2新增记录

insert tbUnRead 

select 1,'Tom'

union

select 2,'Jack'

--3开启事务,并进行更新

 

begin tran

update tbUnRead

set name='Jack_upd'

where ID=2

SELECT @@TRANCOUNT

set Transaction isolation level read committed

--6查询数据,由于当前事务没有提交,所以无法查询数据

select * from tbUnRead where ID=2

6查询数据的结果 如下图:

 

 

 

示例二:设置READ_COMMITTED_SNAPSHOT为ON

use master

go

---创建测试数据库

create database read_committed_SNAPSHOT_Test

go

---激活数据行版本控制

alter database read_committed_SNAPSHOT_Test  set read_committed_SNAPSHOT on

go

 

use read_committed_SNAPSHOT_Test

go

 

--1.创建测试表

create table tbReadLevel

(ID INT,

name nvarchar(20)

)

 

--2新增记录

insert tbReadLevel

select 1,'测试'

go

select ID,name as "修改前数据from tbReadLevel

如下图:

 

go

--3开启事务,并进行更新

 

begin tran

update tbReadLevel

set name='Jack_upd'

where ID=1

SELECT @@TRANCOUNT

 

--查询数据,查询到的数据是上一次提交的数据

select * from tbReadLevel where ID=1

 5的查询结果如下图:

 

(三)可重复读

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)

How to install, uninstall, and reset Windows server backup How to install, uninstall, and reset Windows server backup Mar 06, 2024 am 10:37 AM

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

Lock wait timeout exceeded; try restarting transaction - How to solve MySQL error: transaction wait timeout Lock wait timeout exceeded; try restarting transaction - How to solve MySQL error: transaction wait timeout Oct 05, 2023 am 08:46 AM

Lockwaittimeoutexceeded;tryrestartingtransaction - How to solve the MySQL error: transaction wait timeout. When using the MySQL database, you may sometimes encounter a common error: Lockwaittimeoutexceeded;tryrestartingtransaction. This error indicates that the transaction wait timeout. This error usually occurs when

MySQL transaction processing: the difference between automatic submission and manual submission MySQL transaction processing: the difference between automatic submission and manual submission Mar 16, 2024 am 11:33 AM

MySQL transaction processing: the difference between automatic submission and manual submission. In the MySQL database, a transaction is a set of SQL statements. Either all executions are successful or all executions fail, ensuring the consistency and integrity of the data. In MySQL, transactions can be divided into automatic submission and manual submission. The difference lies in the timing of transaction submission and the scope of control over the transaction. The following will introduce the difference between automatic submission and manual submission in detail, and give specific code examples to illustrate. 1. Automatically submit in MySQL, if it is not displayed

Overview of operator overloading problems and solutions in C++ Overview of operator overloading problems and solutions in C++ Oct 08, 2023 pm 01:02 PM

Overview of Operator Overloading Problems and Solutions in C++ Introduction: Operator overloading is an important feature of the C++ language, which allows programmers to customize existing operators to operate custom data types. However, operator overloading needs to be used with caution, because if used improperly or excessively, it will lead to problems such as reduced code readability, ambiguity, and reduced efficiency. This article will outline common problems with operator overloading in C++ and provide corresponding solutions and code examples. 1. Problems with operator overloading 1.1 Ambiguity problem in operator overloading

PHP PDO Tutorial: An Advanced Guide from Basics to Mastery PHP PDO Tutorial: An Advanced Guide from Basics to Mastery Feb 19, 2024 pm 06:30 PM

1. Introduction to PDO PDO is an extension library of PHP, which provides an object-oriented way to operate the database. PDO supports a variety of databases, including Mysql, postgresql, oracle, SQLServer, etc. PDO enables developers to use a unified API to operate different databases, which allows developers to easily switch between different databases. 2. PDO connects to the database. To use PDO to connect to the database, you first need to create a PDO object. The constructor of the PDO object receives three parameters: database type, host name, database username and password. For example, the following code creates an object that connects to a mysql database: $dsn="mysq

Detailed explanation of the location of win10 virus isolation Detailed explanation of the location of win10 virus isolation Dec 25, 2023 pm 01:45 PM

Files isolated by Win10 viruses are generally stored in fixed locations. Many users want to open the quarantined files and restore their own files, but they don’t know where they are stored. In fact, they can usually be found in the isolation folder of the C drive security software. Where is the win10 virus quarantine file? Answer: In the C:\ProgramData\Microsoft\WindowsDefender\Quarantine folder, we can open this computer and directly copy and paste the file path to the path bar above to find it. Introduction to the virus isolation folder in win10: 1. In win10, the "Quarantine" file usually requires certain permissions to open. It is recommended to open the file as an administrator. 2,

Analysis of solutions to transaction management problems encountered in MongoDB technology development Analysis of solutions to transaction management problems encountered in MongoDB technology development Oct 08, 2023 am 08:15 AM

Analysis of solutions to transaction management problems encountered in MongoDB technology development As modern applications become more and more complex and large, the transaction processing requirements for data are also getting higher and higher. As a popular NoSQL database, MongoDB has excellent performance and scalability in data management. However, MongoDB is relatively weak in data consistency and transaction management, posing challenges to developers. In this article, we will explore the transaction management issues encountered in MongoDB development and propose some solutions.

How does Java database connection handle transactions and concurrency? How does Java database connection handle transactions and concurrency? Apr 16, 2024 am 11:42 AM

Transactions ensure database data integrity, including atomicity, consistency, isolation, and durability. JDBC uses the Connection interface to provide transaction control (setAutoCommit, commit, rollback). Concurrency control mechanisms coordinate concurrent operations, using locks or optimistic/pessimistic concurrency control to achieve transaction isolation to prevent data inconsistencies.

See all articles