SQL Server 事务和锁详解
本文章简单的介绍了关于SQL Server,事务和锁的常见问题与用法同时为初学者提供好的参考意见,有需要的可以参考一下。
最近在项目中进行压力测试遇到了的死锁问题,简言之,如下的代码在 SERIALIZABLE 隔离级别造成了死锁:
代码如下 | 复制代码 |
SELECT @findCount=COUNT(id) FROM MyTable WHERE [fk_related_id]=@Argument IF (@findCount > 0) BEGIN ROLLBACK TRANSACTION RETURN ERROR_CODE END INSERT INTO MyTable ([fk_related_id],…) VALUES (@Argument,…) COMMIT TRANSACTION RETURN SUCCESS_CODE Copy after login |
SQL Server 中锁的种类(Schema操作就暂时不涉及了)
锁类型 | 描述 |
(Shared Lock) | 用于只读操作数据锁定 |
(Update Lock) | 用于数据的更新,在数据真正的需要更新的时候会申请升级为X锁。 |
X(Exclusive Lock) | 独占锁,用于数据的更改。 |
Key-Range Lock(稍后讨论) | 仅仅在 Serializable 隔离级别保护数据,以避免任何有可能使得本事务第二次读取信息产生错误的数据插入操作 |
各个事务隔离级别下锁的使用
SQL Server 中有四种事务隔离级别,具体的大家去参建 MSDN。下面列出在不同的事务隔离级别下这些锁是如何使用的:
隔离级别 | 读数据锁状态 | 写数据锁状态 | 锁持有时间 |
Read Uncommitted | 不获得任何锁 | 不获得任何锁 | |
Read Committed | 数据获得S锁 | 对于 INSERT、DELETE、UPDATE的执行,获得X锁;对于UPDATE的标记,获得U锁; | 读完即释放,并不持有至事务结束。 |
Repeatable Read | 数据获得S锁 | 对于 INSERT、DELETE、UPDATE的执行,获得X锁;对于UPDATE的标记,获得U锁; | 持有至事务结束 |
Serializable | 数据获得S锁,同时获得Key-Range锁。 | 对于 INSERT、DELETE、UPDATE的执行,获得X锁;对于UPDATE的标记,获得U锁,同时获得Key-Range锁。 | 持有至事务结束 |
我们可以利用这些知识形象说明各个隔离级别下的数据一致性:
Read Uncommitted 级别
(1)脏读
(2)更新丢失
(3)不可重复读
(4)幻读
Read Committed 级别
(1)脏读
(2)更新丢失
(3)不可重复读
(4)幻读
Repeatable Read 级别
(1)脏读
(2)更新丢失
(3)不可重复读
(4)幻读
Serializable 级别
(1)脏读
(2)更新丢失
(3)不可重复读
(4)幻读
我们从上图可以比较直观的看到以下的结论
脏读 | 更新丢失 | 不可重复读 | 幻读 | |
Read Uncommitted | 可能 | 可能 | 可能 | 可能 |
Read Committed | 不可能 | 可能 | 可能 | 可能 |
Repeatable Read | 不可能 | 不可能 | 不可能 | 可能 |
Serializable | 不可能 | 不可能 | 不可能 | 不可能 |
这一篇到此为止,下一篇详细介绍 Key-Range Lock 并分析开篇提到的死锁问题

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Microsoft SQL Server is a relational database management system launched by Microsoft. It is a comprehensive database platform that uses integrated business intelligence (BI) tools to provide enterprise-level data management. It is easy to use, has good scalability, and has a high degree of integration with related software. High advantages. The SQL Server database engine provides more secure and reliable storage functions for relational data and structured data, allowing users to build and manage highly available and high-performance data applications for business.

SQLServer or MySQL? The latest research reveals the best database selection. In recent years, with the rapid development of the Internet and big data, database selection has become an important issue faced by enterprises and developers. Among many databases, SQL Server and MySQL, as the two most common and widely used relational databases, are highly controversial. So, between SQLServer and MySQL, which one should you choose? The latest research sheds light on this problem for us. First, let

With the popularity of the Internet, website and application development has become the main business of many companies and individuals. PHP and SQLServer database are two very important tools. PHP is a server-side scripting language that can be used to develop dynamic websites; SQL Server is a relational database management system developed by Microsoft and has a wide range of application scenarios. In this article, we will discuss the development of PHP and SQL Server, as well as their advantages, disadvantages and application methods. First, let's

Introduction to how to use PDO to connect to a Microsoft SQL Server database: PDO (PHPDataObjects) is a unified interface for accessing databases provided by PHP. It provides many advantages, such as implementing an abstraction layer of the database and making it easy to switch between different database types without modifying a large amount of code. This article will introduce how to use PDO to connect to a Microsoft SQL Server database and provide some related code examples. step

In web development, the combination of PHP and MySQL is very common. However, in some cases, we need to connect to other types of databases, such as SQL Server. In this article, we will cover five different ways to connect to SQL Server using PHP.

SQL Server vs. MySQL: Which database is more suitable for high availability architecture? In today's data-driven world, high availability is one of the necessities for building reliable and stable systems. As the core component of data storage and management, the database's high availability is crucial to the business operation of the enterprise. Among the many databases, SQLServer and MySQL are common choices. So in terms of high availability architecture, which database is more suitable? This article will compare the two and give some suggestions.

With the continuous development of the Internet, database selection has become increasingly important. Among the many databases, SQLServer and MySQL are two high-profile options. SQLServer is a relational database management system developed by Microsoft, while MySQL is an open source relational database management system. So how to choose the best database solution between SQLServer and MySQL? First, we can compare these two databases in terms of performance. SQLServer is processing

SQLServer vs MySQL: Which is better for mobile app development? With the rapid development of the mobile application market, it has become increasingly critical for developers to choose a database management system suitable for mobile application development. Among the many choices, SQLServer and MySQL are two database systems favored by developers. This article will focus on comparing these two database systems to determine which is better for mobile application development, and demonstrate their differences through code examples. SQLServer is Microsoft
