Running this same script in 2 windows in SSMS one after another with a couple of seconds' delay.
The second instance that I start later completes but the first one remains locked till I close the second window. What is happening here? Why is it not releasing the lock after the 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
Edit:
The table structure is as follows:
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]
You should update your question with the table structure.
As you did not specify whether your table has any index on it I assume there hasn't, ie dbo.t1
is a heap
.
In this case you'll get classic deadlock
:
session1
wants to select
from table and needs S
for any row it reads and because there is no index
on dbo.t1
the whole table is read and S-lock
on the table is held for the whole duration of the transaction.
In meanwhile session2
does the same and it also acquires S-lock
on the table and holds it.
Session1
now needs to convert its S-lock
to IX
in order to do update
and it is blocked by session2
that holds S
on dbo.t1
.
When session2
tries to do the same it leads to deadlock because both sessions need IX
and both are locked by other session.
Here is the corresponding deadlock graph:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.