当1个进程执行删除查询而2个进程进行select查询时,我遇到了密钥死锁。
经过一番调查后,我发现在进程2中使用select查询会发生额外的S Key锁定而且它没有被释放。即使在空桌子上也会发生这种额外的锁定 下面我从空表中记录了日志示例
66 acquiring IS lock on OBJECT: 6:1614680850:0 (class bit0 ref1) result: OK
Process 66 acquiring IS lock on PAGE: 6:1:16596 (class bit0 ref1) result: OK
Process 66 acquiring S lock on KEY: 6:72057594126925824 (fca2a895105d) (class bit1000000 ref1) result: OK
Process 66 releasing lock on PAGE: 6:1:16596
(0 row(s) affected)
(1 row(s) affected)
Process 66 releasing lock on OBJECT: 6:1614680850:0
此锁未释放,我猜是导致死锁的原因
锁定KEY:6:72057594126925824(fca2a895105d)(类bit1000000 ref1) 出现并且不释放资源
除了可以提供帮助的重要说明:
锁定停止以重新创建重新创建关键索引
删除表格中的任何项目后会出现此锁定,并在每个选择查询中重现
表上的索引只是Id
这是索引pk-404的防御:
CONSTRAINT [PK-404] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS =
ON) ON [PRIMARY]
这是死锁xml:
<deadlock-list>
<deadlock victim="process42dfb48c8">
<process-list>
<process id="process42dfb48c8" taskpriority="0" logused="0" waitresource="KEY: 6:72057594123845632 (a0db81c60a00)" waittime="7738" ownerId="175339832" transactionname="user_transaction" lasttranstarted="2018-01-08T13:37:22.097" XDES="0x46fd24f20" lockMode="S" schedulerid="1" kpid="13560" status="suspended" spid="64" sbid="0" ecid="0" priority="0" trancount="1" lastbatchstarted="2018-01-08T13:37:22.100" lastbatchcompleted="2018-01-08T13:37:22.097" lastattention="1900-01-01T00:00:00.097" clientapp=".Net SqlClient Data Provider" hostname="CHARNIAUSKI" hostpid="25760" loginname="S-1-9-3-4196599067-1192152927-2838817951-2017651452" isolationlevel="read committed (2)" xactid="175339832" currentdb="6" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<frame procname="adhoc" line="1" stmtstart="1166" stmtend="6160" sqlhandle="0x020000004fb4de1aaf9674f427de347416278bb96066c87b0000000000000000000000000000000000000000">unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">unknown </frame>
</executionStack>
<inputbuf>(@$t_perm_tp_0 nvarchar(7),@$t_perm_tp_1 nvarchar(12),@$t_iden_list_2 ntext,@$t_ip_addr_3 nvarchar(3),@$t_cur_user_id_4 char(32),@$t_pwarn_5 nvarchar(24),@$Crtr char(32),@$Mngr char(32),@$Ownr char(32),@SYNCHRONIZATION_STATUS_created_by_id_9 nvarchar(4),@$intCanGet int,@SYNCHRONIZATION_STATUS_current_state_11 nvarchar(16),@SYNCHRONIZATION_STATUS_destination_system_12 nvarchar(15),@SYNCHRONIZATION_STATUS_id_13 nvarchar(22),@SYNCHRONIZATION_STATUS_itemtype_id_14 nvarchar(8),@SYNCHRONIZATION_STATUS_locked_by_id_15 nvarchar(4),@SYNCHRONIZATION_STATUS_modified_by_id_16 nvarchar(4))SELECT [SYNCHRONIZATION_STATUS].[CREATED_BY_ID] [CREATED_BY_ID], fk0.keyed_name AS created_by_id_$_keyed_name, @SYNCHRONIZATION_STATUS_created_by_id_9 AS created_by_id_$_type, @$intCanGet AS created_by_id_$_permission, [SYNCHRONIZATION_STATUS].[CREATED_ON] [CREATED_ON], [SYNCHRONIZATION_STATUS].[CSS] [CSS], [SYNCHRONIZATION_STATUS].[CURRENT_STATE] [CURRENT_STATE], fk1.keyed_name AS current_state_$_keyed_name, @SYNCHRONIZATION_ </inputbuf>
</process>
<process id="process4021d5088" taskpriority="0" logused="75192" waitresource="KEY: 6:72057594123845632 (34ae5dff07bc)" waittime="838" ownerId="175321005" transactionname="user_transaction" lasttranstarted="2018-01-08T13:37:20.910" XDES="0x46fec0f20" lockMode="X" schedulerid="2" kpid="5968" status="suspended" spid="69" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-08T13:37:29.003" lastbatchcompleted="2018-01-08T13:37:29.003" lastattention="1900-01-01T00:00:00.003" clientapp=".Net SqlClient Data Provider" hostname="CHARNIAUSKI" hostpid="25760" loginname="S-1-9-3-4196599067-1192152927-2838817951-2017651452" isolationlevel="read committed (2)" xactid="175321005" currentdb="6" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="28" stmtend="136" sqlhandle="0x02000000a2bf4a1340bcb59dc4a28a98762fb48a43cbf6a00000000000000000000000000000000000000000">unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">unknown </frame>
</executionStack>
<inputbuf>(@id char(32))DELETE FROM [SYNCHRONIZATION_STATUS] WHERE [ID] = @id </inputbuf>
</process>
</process-list>
<resource-list>
<keylock hobtid="72057594123845632" dbid="6" objectname="KHI-SP11-Rel-0103.SYNCHRONIZATION_STATUS" indexname="pk4044" id="lock3d7a81600" mode="X" associatedObjectId="72057594123845632">
<owner-list>
<owner id="process4021d5088" mode="X"/>
</owner-list>
<waiter-list>
<waiter id="process42dfb48c8" mode="S" requestType="wait"/>
</waiter-list>
</keylock>
<keylock hobtid="72057594123845632" dbid="6" objectname="KHI-SP11-Rel-0103.SYNCHRONIZATION_STATUS" indexname="pk4044" id="lock438cc6280" mode="S" associatedObjectId="72057594123845632">
<owner-list>
<owner id="process42dfb48c8" mode="S"/>
</owner-list>
<waiter-list>
<waiter id="process4021d5088" mode="X" requestType="wait"/>
</waiter-list>
</keylock>
</resource-list>
</deadlock>
</deadlock-list>