Avatar of Mike Eghtebas
Mike Eghtebas
Flag for United States of America asked on

Deadlock simulation example...

Following an example from a book displayed on the image, I am executing code a and b given below starting at steps:
1a, 1b, 2a, 2c, 3a, 3c, etc.

Question: Why am I not coming across <blocked> at step 6b?  Please see the image for 6b.
TranBlockedCose a:
--1a
use TSQL2012;
begin tran
select '--1a:   ' + cast(@@TRANCOUNT as varchar(10)), @@TRANCOUNT

--2a 
update hr.Employees 
set phone= N'10004'
where empid= 1
select '--2a:   ' + cast(@@TRANCOUNT as varchar(10)), @@TRANCOUNT

--3a 
--(excute next in b)

--4a
update Production.Suppliers 
set fax= N'555-1212'
where supplierid= 1
select '--4a:   ' + cast(@@TRANCOUNT as varchar(10)), @@TRANCOUNT
--5a
--<blocked>

--6a 
-- (excute next in b)

-- 7a
select '--7a:   ' + cast(@@TRANCOUNT as varchar(10)), @@TRANCOUNT
if @@TRANCOUNT >0 rollback;
select '--7aa:   ' + cast(@@TRANCOUNT as varchar(10)), @@TRANCOUNT

Open in new window


Code b:
--1b
use TSQL2012;
begin tran;
select '--1b:   ' + cast(@@TRANCOUNT as varchar(10)), @@TRANCOUNT

--2b
--(excute next in a)

--3b
update Production.Suppliers 
set fax= N'555-1216'
where supplierid= 1
select '--3b:   ' + cast(@@TRANCOUNT as varchar(10)), @@TRANCOUNT

--4b
--(excute next in a)

--5b
update hr.Employees 
set phone= N'555-9999'
where empid= 1;
select '--5b:   ' + cast(@@TRANCOUNT as varchar(10)), @@TRANCOUNT

--6b
--<blocked>

--7b
select '--7b:   ' + cast(@@TRANCOUNT as varchar(10)), @@TRANCOUNT
if @@TRANCOUNT >0 rollback;
select '--7bb:   ' + cast(@@TRANCOUNT as varchar(10)), @@TRANCOUNT

Open in new window

Microsoft SQL ServerMicrosoft SQL Server 2008Microsoft SQL Server 2005

Avatar of undefined
Last Comment
Vitor Montalvão

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ste5an

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mike Eghtebas

ASKER
It seems 6b (<blocked>) per the book is not correct because there is no block at 6b.
ste5an

6b is misleading, cause the b thread is already waiting in 5b. Here it wants to update Employees, but it is waiting, cause 2a has still its lock from its update.
SOLUTION
Vitor Montalvão

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mike Eghtebas

ASKER
Vitor,

re:> Because it's a deadlock and the engine already chose 'A' to be killed.

I am following the example in the book. Coming across to <blocked> at step 6b is expected per the book I am reading?

Is your conclusion "The book is wrong?"

If you find that the book is wrong, I am okay with that. But if you find the book is correct then my original question remain not answered (Why am I not coming across <blocked> at step 6b?)
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Vitor Montalvão

Sorry, I was off on vacation.
The book is not wrong (at least if the table you post in your original question is from the book). Between points 6 and 7 it says:
"one transaction finnishes and the other transaction is aborted", so there's no human intervention, like I said before. Is the engine who chose which one to abort/kill.