Database deadlock (transfer)

Speaking of deadlock in data, it has been encountered many times in written test questions. Today, I hereby make a summary of database deadlocks, in order to avoid future troubles, hoohoo!

First, let's look at a few definitions:

 

1. Deadlock

The so-called deadlock: refers to a phenomenon in which two or more processes are waiting for each other due to competition for resources during the execution process. If there is no external force, they will not be able to advance. At this time, the system is said to be in a deadlock state or the system has a deadlock, and these processes that are always waiting for each other are called deadlock processes. Because the resource occupation is mutually exclusive, when a process requests resources, the relevant process will never be able to allocate the necessary resources and cannot continue to run without external assistance, which results in a special phenomenon of deadlock.
 

2. Database

A database is a warehouse that organizes, stores and manages data according to the data structure .
 

3. Necessary conditions for deadlock

  •  Mutual exclusion condition: refers to the exclusive use of the allocated resources by a process, that is, a resource is only occupied by one process within a period of time. If there are other processes requesting resources at this time, the requester can only wait until the process occupying the resources is used up and released.
  • Request and hold conditions: Refers to a process that has held at least one resource, but a new resource request is made, and the resource has been occupied by other processes. At this time, the requesting process is blocked, but it keeps other resources obtained by itself.
  • Non-deprivation condition: Refers to the resource that the process has obtained, it cannot be deprived before it is used up, and it can only be released by itself when it is used up.
  • Loop waiting condition: When a deadlock occurs, there must be a process-resource loop chain, that is, P0 in the process set {P0, P1, P2, ···, Pn} is waiting for a resource occupied by P1; P1 is waiting for resources occupied by P2, ..., Pn is waiting for resources already occupied by P0.

4. The basic way to deal with deadlock

      1) Prevent deadlock.
 
  This is a simpler and more intuitive method of prior prevention. The method is to prevent deadlock by setting certain restrictions to destroy one or more of the four necessary conditions for deadlock. Deadlock prevention is an easier method to implement and has been widely used. However, the imposed constraints are often too strict, which may result in lower system resource utilization and system throughput.
 
  2) Avoid deadlocks.
 
  This method is also a preventive strategy, but it does not need to take various restrictive measures in advance to destroy the four necessary conditions for deadlock, but in the dynamic allocation process of resources, use a certain method to prevent the system Enter an unsafe state to avoid deadlocks.
 
  3) Detect deadlock.
 
  This method does not require any restrictive measures in advance, nor does it need to check whether the system has entered an unsafe area, this method allows the system to deadlock during operation. However, the detection mechanism set by the system can detect the occurrence of deadlock in time, and accurately determine the processes and resources related to the deadlock, and then take appropriate measures to clear the deadlock that has occurred from the system.
 
  4) Release the deadlock.
 
  This is a measure that goes along with detecting deadlocks. When it is detected that a deadlock has occurred in the system, the process must be released from the deadlock state. A common implementation method is to revoke or suspend some processes in order to reclaim some resources, and then allocate these resources to the processes that are already in the blocked state and make them ready to continue running. Deadlock detection and removal measures may enable the system to obtain better resource utilization and throughput, but it is also the most difficult to achieve.
 

Second, what is the deadlock in the database!

 

1. The mechanism of SQL server lock

SQL server的所有活动都会产生锁。锁定的单元越小,就越能越能提高并发处理能力,但是管理锁的开销越大。如何找到平衡点,使并发性和性能都可接受是SQL Server的难点。
SQL Server有如下几种琐:
1、 共享锁
用于只读操作(SELECT),锁定共享的资源。共享锁不会阻止其他用户读,但是阻止其他的用户写和修改。
2、 更新锁
更新锁是一种意图锁,当一个事物已经请求共享琐后并试图请求一个独占锁的时候发生更新琐。例如当两个事物在几行数据行上都使用了共享锁,并同时试图获取独占锁以执行更新操作时,就发生了死锁:都在等待对方释放共享锁而实现独占锁。更新锁的目的是只让一个事物获得更新锁,防止这种情况的发生。
3、 独占锁(排它锁)
一次只能有一个独占锁用在一个资源上,并且阻止其他所有的锁包括共享缩。写是独占锁,可以有效的防止’脏读’
4、 意图锁
在使用共享锁和独占锁之前,使用意图锁。从表的层次上查看意图锁,以判断事物能否获得共享锁和独占锁,提高了系统的性能,不需从爷或者行上检查。
5、 计划锁
Sch-M,Sch-S。对数据库结构改变时用Sch-M,对查询进行编译时用Sch-S。这两种锁不会阻塞任何事物锁,包括独占锁。

读是共享锁,写是排他锁,先读后更新的操作是更新锁,更新锁成功并且改变了数据时更新锁升级到排他锁。

 

2.下面我们看下如何避免死锁

 
1 使用事务时,尽量缩短事务的逻辑处理过程,及早提交或回滚事务;
2 设置死锁超时参数为合理范围,如:3分钟-10分种;超过时间,自动放弃本次操作,避免进程悬挂;
3 所有的SP都要有错误处理(通过@error
4 一般不要修改SQL SERVER事务的默认级别。不推荐强行加锁
5 优化程序,检查并避免死锁现象出现;
1)合理安排表访问顺序
2)在事务中尽量避免用户干预,尽量使一个事务处理的任务少些。
3)采用脏读技术。脏读由于不对被访问的表加锁,而避免了锁冲突。在客户机/服务器应用环境中,有些事务往往不允许读脏数据,但在特定的条件下,我们可以用脏读。
4)数据访问时域离散法。数据访问时域离散法是指在客户机/服务器结构中,采取各种控制手段控制对数据库或数据库中的对象访问时间段。主要通过以下方式实现: 合理安排后台事务的执行时间,采用工作流对后台事务进行统一管理。工作流在管理任务时,一方面限制同一类任务的线程数(往往限制为1个),防止资源过多占用; 另一方面合理安排不同任务执行时序、时间,尽量避免多个后台任务同时执行,另外,避免在前台交易高峰时间运行后台任务
5)数据存储空间离散法。数据存储空间离散法是指采取各种手段,将逻辑上在一个表中的数据分散到若干离散的空间上去,以便改善对表的访问性能。主要通过以下方法实现: 第一,将大表按行或列分解为若干小表; 第二,按不同的用户群分解。
6)使用尽可能低的隔离性级别。隔离性级别是指为保证数据库数据的完整性和一致性而使多用户事务隔离的程度,SQL92定义了4种隔离性级别:未提交读、提交读、可重复读和可串行。如果选择过高的隔离性级别,如可串行,虽然系统可以因实现更好隔离性而更大程度上保证数据的完整性和一致性,但各事务间冲突而死锁的机会大大增加,大大影响了系统性能。
7)使用Bound Connections。Bound connections 允许两个或多个事务连接共享事务和锁,而且任何一个事务连接要申请锁如同另外一个事务要申请锁一样,因此可以允许这些事务共享数据而不会有加锁的冲突。
8)考虑使用乐观锁定或使事务首先获得一个独占锁定。 

 

 

大家看下下面的情况是否会出现死锁呢?
 
A. select , update,select
B.select,select,update
C.select,update,update
D.update,select,update
上面这四种情况哪种会出现死锁,哪种出现死锁呢?你的理由又是什么呢?
 

3.下面我们来看下下面的这个死锁问题!

[sql]  view plain  copy
 
 print ?
  1. <span style="font-family:'Microsoft YaHei';">-- A事务先更新table1表,然后延时30秒,再更新table2表;  
  2. begin tran  
  3. update table1 set A='aa' where B='b2';  
  4. --这将在 Table1 中生成排他行锁,直到事务完成后才会释放该锁。  
  5. waitfor delay '00:00:30';  
  6. --进入延时  
  7. update table2 set D='d5' where E='e1' ;  
  8. commit tran  
  9. -- B事务先更新table2表,然后延时10秒,再更新table1表;  
  10. begin tran  
  11. update table2 set D='d5' where E='e1';  
  12. --这将在 Table2 中生成排他行锁,直到事务完成后才会释放该锁  
  13. waitfor delay '00:00:10'  
  14. --进入延时  
  15. update table1 set A='aa'where B='b2' ;   
  16. commit tran</span>  
 
Transaction A first updates table1 table, then delays for 30 seconds, and then updates table2 table; begin tranupdate table1 set A='aa' where B='b2';--This will generate an exclusive row lock in Table1 until the transaction is completed will release the lock. waitfor delay '00:00:30';--Enter delay update table2 set D='d5' where E='e1' ;commit tran-- Transaction B first updates table2 table, then delays for 10 seconds, and then updates table1 table;begin tranupdate table2 set D='d5' where E='e1';--this will generate an exclusive row lock in Table2, which will not be released until the transaction completes waitfor delay '00:00:10'-- Entry delay update table1 set A='aa' where B='b2' ;commit tran
If the above two transactions are executed concurrently, both transactions A and B must wait for the other party to release the exclusive lock, thus forming a deadlock.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326946513&siteId=291194637