SQL Server数据库表锁定原理以及如何解除表的锁定
1. 数据库表锁定原理 1.1 目前的C/S,B/S结构都是多用户访问数据库,每个时间点会有成千上万个user来访问DB,其中也会同时存取同一份数据,会造成数据的不一致性或者读脏数据. 1.2 事务的ACID原则 1.3 锁是关系数据库很重要的一部分, 数据库必须有锁的机制来确保
1. 数据库表锁定原理
1.1 目前的C/S,B/S结构都是多用户访问数据库,每个时间点会有成千上万个user来访问DB,其中也会同时存取同一份数据,会造成数据的不一致性或者读脏数据.
1.2 事务的ACID原则
1.3 锁是关系数据库很重要的一部分, 数据库必须有锁的机制来确保数据的完整和一致性.
1.3.1 SQL Server中可以锁定的资源:
1.3.2 锁的粒度:
1.3.3 锁的升级:
锁的升级门限以及锁升级是由系统自动来确定的,不需要用户设置.
1.3.4 锁的类型:
(1) 共享锁:
共享锁用于所有的只读数据操作.
(2) 修改锁:
修改锁在修改操作的初始化阶段用来锁定可能要被修改的资源,这样可以避免使用共享锁造成的死锁现象
(3) 独占锁:
独占锁是为修改数据而保留的。它所锁定的资源,其他事务不能读取也不能修改。独占锁不能和其他锁兼容。
(4) 架构锁
结构锁分为结构修改锁(Sch-M)和结构稳定锁(Sch-S)。执行表定义语言操作时,SQL Server采用Sch-M锁,编译查询时,SQL Server采用Sch-S锁。
(5) 意向锁
意向锁说明SQL Server有在资源的低层获得共享锁或独占锁的意向。
(6) 批量修改锁
批量复制数据时使用批量修改锁
1.3.4 SQL Server锁类型
(1) HOLDLOCK: 在该表上保持共享锁,直到整个事务结束,而不是在语句执行完立即释放所添加的锁。
(2) NOLOCK:不添加共享锁和排它锁,当这个选项生效后,可能读到未提交读的数据或“脏数据”,这个选项仅仅应用于SELECT语句。
(3) PAGLOCK:指定添加页锁(否则通常可能添加表锁)。
(4) READCOMMITTED用与运行在提交读隔离级别的事务相同的锁语义执行扫描。默认情况下,SQL Server 2000 在此隔离级别上操作。
(5) READPAST: 跳过已经加锁的数据行,这个选项将使事务读取数据时跳过那些已经被其他事务锁定的数据行,而不是阻塞直到其他事务释放锁,
READPAST仅仅应用于READ COMMITTED隔离性级别下事务操作中的SELECT语句操作。
(6) READUNCOMMITTED:等同于NOLOCK。
(7) REPEATABLEREAD:设置事务为可重复读隔离性级别。
(8) ROWLOCK:使用行级锁,而不使用粒度更粗的页级锁和表级锁。
(9) SERIALIZABLE:用与运行在可串行读隔离级别的事务相同的锁语义执行扫描。等同于 HOLDLOCK。
(10) TABLOCK:指定使用表级锁,而不是使用行级或页面级的锁,SQL Server在该语句执行完后释放这个锁,而如果同时指定了HOLDLOCK,该锁一直保持到这个事务结束。 (11) TABLOCKX:指定在表上使用排它锁,这个锁可以阻止其他事务读或更新这个表的数据,直到这个语句或整个事务结束。
(12) UPDLOCK :指定在读表中数据时设置更新 锁(update lock)而不是设置共享锁,该锁一直保持到这个语句或整个事务结束,使用UPDLOCK的作用是允许用户先读取数据(而且不阻塞其他用户读数据),并且保证在后来再更新数据时,这一段时间内这些数据没有被其他用户修改。
(本段摘自CSDN博客: http://blog.csdn.net/zp752963831/archive/2009/02/18/3906477.aspx)
2. 如何解除表的锁定,解锁就是要终止锁定的那个链接,或者等待该链接事务释放.
2.1 Activity Monitor
可以通过Wait Type, Blocked By栏位查看到,SPID 54 被SPID 53 阻塞. 可以右键Details查到详细的SQL 语句,或Kill掉这个进程.
2.2 SQL Server提供几个DMV,查看locks
sys.dm_exec_requests
sys.dm_tran_locks
sys.dm_os_waiting_tasks
sys.dm_tran_database_transactions
(1)select*from sys.dm_tran_locks
where resource_type'DATABASE'--and resource_database_id=DB_ID()
(2)
<p><span>SELECT</span><span> session_id, blocking_session_id,</span><span>*</span><span>FROM</span><span> sys.dm_exec_requests </span><span>WHERE</span><span> blocking_session_id </span><span>></span><span>0</span></p>
(3)
代码
<p><span>SELECT</span><span> request_session_id </span><span>as</span><span> Spid, </span><span>Coalesce</span><span>(s.name </span><span>+</span><span>'</span><span>.</span><span>'</span><span>+</span><span> o.name </span><span>+</span><span>isnull</span><span>(</span><span>'</span><span>.</span><span>'</span><span>+</span><span> i.name,</span><span>''</span><span>), s2.name </span><span>+</span><span>'</span><span>.</span><span>'</span><span>+</span><span> o2.name, db.name) </span><span>AS</span><span> Object, l.resource_type </span><span>as</span><span> Type, request_mode </span><span>as</span><span> Mode, request_status </span><span>as</span><span> Status </span><span>FROM</span><span> sys.dm_tran_locks l </span><span>LEFT</span><span>JOIN</span><span> sys.partitions p </span><span>ON</span><span> l.resource_associated_entity_id </span><span>=</span><span> p.hobt_id </span><span>LEFT</span><span>JOIN</span><span> sys.indexes i </span><span>ON</span><span> p.</span><span>object_id</span><span>=</span><span> i.</span><span>object_id</span><span>AND</span><span> p.index_id </span><span>=</span><span> i.index_id </span><span>LEFT</span><span>JOIN</span><span> sys.objects o </span><span>ON</span><span> p.</span><span>object_id</span><span>=</span><span> o.</span><span>object_id</span><span>LEFT</span><span>JOIN</span><span> sys.schemas s </span><span>ON</span><span> o.schema_id </span><span>=</span><span> s.schema_id </span><span>LEFT</span><span>JOIN</span><span> sys.objects o2 </span><span>ON</span><span> l.resource_associated_entity_id </span><span>=</span><span> o2.</span><span>object_id</span><span>LEFT</span><span>JOIN</span><span> sys.schemas s2 </span><span>ON</span><span> o2.schema_id </span><span>=</span><span> s2.schema_id </span><span>LEFT</span><span>JOIN</span><span> sys.databases db </span><span>ON</span><span> l.resource_database_id </span><span>=</span><span> db.database_id </span><span>WHERE</span><span> resource_database_id </span><span>=</span><span>DB_ID</span><span>() </span><span>ORDER</span><span>BY</span><span> Spid, Object, </span><span>CASE</span><span> l.resource_type </span><span>When</span><span>'</span><span>database</span><span>'</span><span>Then</span><span>1</span><span>when</span><span>'</span><span>object</span><span>'</span><span>then</span><span>2</span><span>when</span><span>'</span><span>page</span><span>'</span><span>then</span><span>3</span><span>when</span><span>'</span><span>key</span><span>'</span><span>then</span><span>4</span><span>Else</span><span>5</span><span>end</span></p>

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



HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Douyin, as one of the most popular short video platforms today, has more than 800 million users. Douyin not only provides people with a platform to showcase their talents and share their lives, but also has many practical functions, such as Douyin lock screen. So, how to set the Douyin lock screen? 1. How to set the Douyin lock screen? 1. Open the Douyin APP, enter the homepage, and click the "Me" button in the lower right corner to enter the personal center. 2. On the personal center page, slide the screen upwards, find the "Settings" option, and click to enter. 3. On the settings page, slide down the screen, find the "General" option, and click to enter. 4. On the general page, continue to slide down the screen, find the "Lock Screen" option, and click to enter. 5. On the lock screen page, you can see “Lock screen play
