下面这篇对理解死锁非常重要,首先死锁是如何产生的我们要清楚。

We already know why blocking occurs in the system and howto detect and troubleshoot the blocking issues. Today I’d like us to focus on the deadlocks.First, what is the deadlock? Classic deadlock occurs when 2 processes compete for the resources and waiting on each other. Let’s take a look on that. Click on each picture below to open them in the different window. Let’s assume that Orders table has the clustered index on ID column.Let we have the session 1 that starts transaction and updates the row from the Order table. We already know that Session 1 acquires X lock on this row (ID=1) and hold it till end of transaction

我们已经知道为什么系统中会出现阻塞,也知道如何去探测这些引起阻塞的本质原因。今天我们将重点关注在死锁上。首先,什么是死锁呢?最经典的死锁出现在两个线程为了抢夺同一资源而相互竞争以至于出现你依赖我你依赖我的死循环情况。请看下图的图,我们假定在订单表Id列上有一个聚集索引,会话1开起一个事务去更新订单表的数据。此时会话1会在行数据(ID=1)上申请排它锁至于更新的事务结束。

Now let’s start session 2 and have it update another row in the Orders table. Same situation – session acquires and holds X lock on the row with (ID = 2).

现在,我们开启会话2也更新订单表,不同的是更新的数据行为Id=2,同样的在数据行Id=2上也会申请排它锁至到更新事务的结束。

Now if session 1 tries to acquire X lock on the row with ID = 2 , it cannot do that because Session 2 already held X lock there. So, Session 1 is going to be blocked till Session 2 rollback or commit the transaction

现在,如果会话1尝试在数据行Id=2上申请排它锁,将会受到阻塞,因为会话2已经申请了排它锁。只有当会话2回滚或者提交事务后才能够重新尝试获取排它锁。

It worth to mention that it would happen in any transaction isolation level except SNAPSHOT that handles such conditions differently. So read uncommitted does not help with the deadlocks at all.

这里值得提醒的时,上面的场景需要排除镜像这种特殊优化过的隔离模式。所以从上面的例子来看,read uncommitted隔离模式是不能帮忙我们解决死锁问题的。

Simple enough. Unfortunately it rarely happens in the real life. Let’s see it in the small example using the same dbo.Data table like before. Let’s start 2 sessions and update 2 separate rows in the table (acquire X locks).

足够简单,但上面的场景并不常出现。下面我们采用之前使用数据表,我们打开两个会话去更新不同的数据行。

Session 1:

会话1,更新ID=0的数据行

Session 2:

会话2,更新ID为40000的数据行

Now let’s run 2 selects.

现在我们再运行两个查询语句

Session 1:

会话1,查询Value=1的数据

Session 2:

会话2,查询Value=40001的数据

Well, as you can see, it introduces deadlock.

如下图所示,我们看到了死锁的相关提示

To understand why it happens, let’s take a look at the execution plan of the select statement:

为了理解到底发生了什么,我们看一相查询所产生的实际执行计划

As you can see – there is the table scan. So let’s think what happened

如上图所示,这是一个表扫描,我们来想一下发生了什么:

  1. First of all, we have 2 X locks on the different rows acquired by both sessions.

首先,我们有两个排它锁在不同的数据行上,每个会话占用一个。

  1. When session 1 ran select, it introduced table scan. In read committed, repeatable read or serializable isolation levels, readers issue shared S locks, so when session 1 tried to read the row with ID = 40000 (X lock held by session 2) – it was blocked.

当会话1执行查询时,它会引发表扫描,在三种悲观事务隔离级别事务中(除read uncommitted),读会产生共享锁,所有当会话1尝试去读取Id=40000的数据行时就会出现阻塞,因为会话2更新数据行的事务并未结束。

  1. Same thing happens with session 2 – it’s blocked on the row with ID = 1 (X lock held by session 1).

同样的情况出现在会话2中,当它尝试去读取ID=1的数据行时,此时由于会话1更新的事务并未结束,所以此次查询也会被阻塞。

So this is the classic deadlock even if there are no data updates involved. It worth to mention that read uncommitted transaction isolation level would not introduce deadlock – readers in this isolation level do not acquire S locks. Although you’ll have deadlock in the case if you change select to update even in read uncommitted level. Otimistic isolation levels also behave differently and we will talk about it later.So as you can see, in the real life as other blocking issues deadlocks happen due non-optimized queries.

这是经典的死锁场景即使此时没有任何相关的数据被更新。这里值得再次提醒的是,在read uncommitted事务隔离级别下是不能减少死锁的,尽管在此模式时并不需要申请共享锁。

注:本篇文章中一直来讲read uncommitted模式不能解决死锁,在本篇没有举例子,但之前的系列文章中已经有了,可以详细参考。

Next week we will talk how to detect and deal with deadlocks.

下一次我将讲如何探测死锁以及如何处理死锁问题。

[翻译]:SQL死锁-为什么会出现死锁的更多相关文章

  1. [翻译]:SQL死锁-死锁排除

    As we already saw, the reasons why we have blocking issues and deadlocks in the system are pretty mu ...

  2. [翻译]:SQL死锁-锁的类型

    很久没有写博客了,这里面的原因有很多.最近的一个项目由于客户明确提出要做下性能压力测试,使用的工具就是VS自带的压力测试工具.以前其它项目做压力测试后反馈的其中一个重要问题就是数据库的死锁.没想到我们 ...

  3. [翻译]:SQL死锁-阻塞探测

    到了这篇,才是真正动手解决问题的时候,有了死锁之后就要分析死锁的原因,具体就是需要定位到具体的SQL语句上.那么如何发现产生死锁的问题本质呢?下面这篇讲的非常细了,还提到了不少实用的SQL,但对我个人 ...

  4. 30分钟全面解析-SQL事务+隔离级别+阻塞+死锁

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化.  本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...

  5. SQL Server-聚焦深入理解死锁以及避免死锁建议(三十三)

    前言 终于进入死锁系列,前面也提到过我一直对隔离级别和死锁以及如何避免死锁等问题模棱两可,所以才鼓起了重新学习SQL Server系列的勇气,本节我们来讲讲SQL Server中的死锁,看到许多文章都 ...

  6. SQL Server-聚焦深入理解死锁以及避免死锁建议(转载)

    前言 终于进入死锁系列,前面也提到过我一直对隔离级别和死锁以及如何避免死锁等问题模棱两可,所以才鼓起了重新学习SQL Server系列的勇气,本节我们来讲讲SQL Server中的死锁,看到许多文章都 ...

  7. (4.12)全面解析-SQL事务+隔离级别+阻塞+死锁

    30分钟全面解析-SQL事务+隔离级别+阻塞+死锁 转自:https://blog.csdn.net/slowlifes/article/details/52752735 2016年10月07日 23 ...

  8. (4.7)怎么捕获和记录SQL Server中发生的死锁?

    转自:https://blog.csdn.net/c_enhui/article/details/19498327 怎么捕获和记录SQL Server中发生的死锁? 关键词:死锁记录,死锁捕获 sql ...

  9. (分享别人的一篇好文章,来自jackson0714)30分钟全面解析-SQL事务+隔离级别+阻塞+死锁()

    30分钟全面解析-SQL事务+隔离级别+阻塞+死锁 阅读目录 概述: 一.事务 二.锁 三.阻塞 四.隔离级别 五.死锁 以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQ ...

随机推荐

  1. [原创]Android Handler使用Message的一个注意事项

    最近发现了一个莫名其妙的问题,在使用Handler.post(Runnable)这个接口时,Runnable有时候没有运行,非常奇怪,后来发现是因为调用Handler.removeMessage()时 ...

  2. 【转】你真的了解word-wrap和word-break的区别吗?

    原文在这里: http://www.cnblogs.com/2050/archive/2012/08/10/2632256.html

  3. POJ 2676 Sudoku

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12005   Accepted: 5984   Special ...

  4. 【翻译】C# Tips & Tricks: Weak References - When and How to Use Them

    原文:C# Tips & Tricks: Weak References - When and How to Use Them Sometimes you have an object whi ...

  5. 进阶学习js中的执行上下文

    在js中的执行上下文,菜鸟入门基础 这篇文章中我们简单的讲解了js中的上下文,今天我们就更进一步的讲解js中的执行上下文. 1.当遇到变量名和函数名相同的问题. var a = 10; functio ...

  6. Cwinux简介及用法简述

    我在我的个人博客上发表了一篇文章 Cwinux简介及用法简述 http://apprentice89.com/cwinux_introduction_and_use/

  7. Mina、Netty、Twisted一起学(二):TCP消息边界问题及按行分割消息

    在TCP连接开始到结束连接,之间可能会多次传输数据,也就是服务器和客户端之间可能会在连接过程中互相传输多条消息.理想状况是一方每发送一条消息,另一方就立即接收到一条,也就是一次write对应一次rea ...

  8. springboot themleaf 开发笔记

    <form id="form-query" th:action="@{/member-score/rule-save}" th:object=" ...

  9. 队列的图文解析 和 对应3种语言的实现(C/C++/Java)

    概要 本章和介绍"栈"时的流程一样,先对队列进行介绍,然后分别给出队列的C.C++和Java三种语言的实现.内容包括:1. 队列的介绍2. 队列的C实现3. 队列的C++实现4.  ...

  10. 【转载】Linux动态库搜索路径的技巧

    转自:http://soft.chinabyte.com/os/232/11488732_2.shtml 众所周知,Linux动 态库的默认搜索路径是/lib和/usr/lib.动态库被创建后,一般都 ...