We help IT Professionals succeed at work.
Get Started

Deadlock simulation example...

56 Views
Last Modified: 2015-08-03
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

Comment
Watch Question
Senior Developer
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 2 Answers and 6 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE