Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag 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.
User generated imageCose 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

ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Eghtebas

ASKER

It seems 6b (<blocked>) per the book is not correct because there is no block at 6b.
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?)
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.