在SSMS的2个窗口中依次运行同一脚本,但要延迟几秒钟。
我稍后启动的第二个实例完成,但是第一个实例保持锁定状态,直到我关闭第二个窗口。
这是怎么回事为什么在COMMIT之后不释放锁?
SET TRAN ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
SELECT * FROM dbo.t1
WHERE id IN (1,3)
WAITFOR DELAY '00:00:20'
UPDATE t1 SET InUse=0
WHERE id IN (1,3)
COMMIT
编辑:
表结构如下:
CREATE TABLE [dbo].[t1](
[id] [INT] NOT NULL,
[InUse] [BIT] NOT NULL DEFAULT ((0)),
PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
答案 0 :(得分:1)
您应该使用表结构更新您的问题。
由于您没有指定表上是否有索引,所以我假设没有索引,即dbo.t1
是heap
。
在这种情况下,您会获得经典的deadlock
:
session1
希望从表中select
,并且读取的任何行都需要S
,并且由于index
上没有dbo.t1
,因此整个表都被读取和表上的S-lock
会在整个交易过程中保留。
与此同时,session2
进行了同样的操作,它还获取了表中的S-lock
并将其保存。
Session1
现在需要将其S-lock
转换为IX
才能进行update
,并且被session2
所持有的S
阻止在dbo.t1
上。
session2
尝试执行相同操作时会导致死锁,因为两个会话都需要IX
,并且都被其他会话锁定。
这是对应的死锁图:
deadlock-list
deadlock victim=process155d1d498
process-list
process id=process155d1d498 taskpriority=0 logused=0 waitresource=OBJECT: 26:1765581328:0 waittime=16222 ownerId=5528849 transactionname=user_transaction lasttranstarted=2019-02-07T13:39:38.837 XDES=0x15ec1c3a8 lockMode=IX schedulerid=4 kpid=8140 status=suspended spid=54 sbid=0 ecid=0 priority=0 trancount=2 lastbatchstarted=2019-02-07T13:39:38.833 lastbatchcompleted=2019-02-07T13:39:38.833 lastattention=2019-02-07T13:38:49.187 clientapp=Microsoft SQL Server Management Studio - Query hostname=pppp hostpid=12276 loginname=FINCONSGROUP\anna.savchenko isolationlevel=serializable (4) xactid=5528849 currentdb=26 lockTimeout=4294967295 clientoption1=671098976 clientoption2=390200
executionStack
frame procname=adhoc line=9 stmtstart=248 stmtend=334 sqlhandle=0x02000000f445021276fec0f5ec119082f65611ce316a4d280000000000000000000000000000000000000000
UPDATE t1 SET InUse=0
WHERE id IN (1,3)
inputbuf
SET TRAN ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
SELECT * FROM dbo.t1
WHERE id IN (1,3)
WAITFOR DELAY '00:00:20'
UPDATE t1 SET InUse=0
WHERE id IN (1,3)
COMMIT
process id=process15649f868 taskpriority=0 logused=0 waitresource=OBJECT: 26:1765581328:0 waittime=1212 ownerId=5529084 transactionname=user_transaction lasttranstarted=2019-02-07T13:39:53.847 XDES=0x15ec1d048 lockMode=IX schedulerid=3 kpid=15516 status=suspended spid=57 sbid=0 ecid=0 priority=0 trancount=2 lastbatchstarted=2019-02-07T13:39:53.847 lastbatchcompleted=2019-02-07T13:39:53.847 lastattention=1900-01-01T00:00:00.847 clientapp=Microsoft SQL Server Management Studio - Query hostname=pppp hostpid=12276 loginname=FINCONSGROUP\anna.savchenko isolationlevel=serializable (4) xactid=5529084 currentdb=26 lockTimeout=4294967295 clientoption1=671098976 clientoption2=390200
executionStack
frame procname=adhoc line=9 stmtstart=248 stmtend=334 sqlhandle=0x0200000093b3ba1c08d586d1f142f473a7c8996074a369fc0000000000000000000000000000000000000000
UPDATE t1 SET InUse=0
WHERE id IN (1,3)
inputbuf
SET TRAN ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
SELECT * FROM dbo.t1
WHERE id IN (1,3)
WAITFOR DELAY '00:00:20'
UPDATE t1 SET InUse=0
WHERE id IN (1,3)
COMMIT
resource-list
objectlock lockPartition=0 objid=1765581328 subresource=FULL dbid=26 objectname=parts.dbo.t1 id=lock152c2d300 mode=S associatedObjectId=1765581328
owner-list
owner id=process15649f868 mode=S
owner id=process15649f868 mode=IX requestType=convert
waiter-list
waiter id=process155d1d498 mode=IX requestType=convert
objectlock lockPartition=0 objid=1765581328 subresource=FULL dbid=26 objectname=parts.dbo.t1 id=lock152c2d300 mode=S associatedObjectId=1765581328
owner-list
owner id=process155d1d498 mode=S
owner id=process155d1d498 mode=IX requestType=convert
waiter-list
waiter id=process15649f868 mode=IX requestType=convert