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

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
4a gets a wait, cause Suppliers is locked by 3b. When now 5b is executed, it must wait, cause 2a has still a lock on Employess. Result: Both are waiting for each other to free the lock. But this cannot happen as they wait. A deadlock :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
It seems 6b (<blocked>) per the book is not correct because there is no block at 6b.
0
ste5anSenior DeveloperCommented:
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Vitor MontalvãoMSSQL Senior EngineerCommented:
Why am I not coming across <blocked> at step 6b?
Because it's a deadlock and the engine already chose 'A' to be killed.
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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?)
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.