How do you simulate a deadlock for testing purposes



How do you simulate a deadlock for testing purposes..

Answer / jay

Below is a quick recipe for a dead lock. Two transactions,
one first updating table 1, then 2 and the other one doing
it in reverse order.

Both transactions wait in the middle for 20 seconds to give
you some time to execute them 'simulaneously'.

When you run the two in transactions in two windows 'at the
same time', you'll only have to wait ~20 seconds, and one of
the windows will experience a dead lock.




CREATE TABLE t1 (i int);
CREATE TABLE t2 (i int);

INSERT t1 SELECT 1;
INSERT t2 SELECT 9;


/* in one window enter: */
BEGIN TRAN
UPDATE t1 SET i = 11 WHERE i = 1
WAITFOR DELAY '00:00:20'
UPDATE t2 SET i = 99 WHERE i = 9
COMMIT

/* in a second window (another transaction) enter: */
BEGIN TRAN
UPDATE t2 SET i = 99 WHERE i = 9
WAITFOR DELAY '00:00:20'
UPDATE t1 SET i = 11 WHERE i = 1
COMMIT

Is This Answer Correct ?    8 Yes 1 No

Post New Answer

More SQL Server Interview Questions

How can we solve concurrency problems?

0 Answers  


Explain “not null constraint” in sql server?

0 Answers  


What do you understand by the data quality services in sql server?

0 Answers  


WHAT IS TRIGGERS IN SQL? AND WHAT IS THE BENIFIT?

8 Answers  


Syntax to find the used space and free space of the Data file in a database(SQL Server). Following queries didn't give the exact Used space and Free Space Information sp_spaceused;DBCC showfilestats;exec MyDbName.dbo.sp_spaceused;SP_HELPFILE Can any one tell me the query for how to find the exact used data file space and free space in a Data File?

2 Answers   Cognizant,






what are default? Is there a column to which a default cant be bound?

1 Answers  


What are xml indexes?

0 Answers  


Write a query to find 5th highest amount paid from the customer table.

0 Answers  


Describe and explain about SQL native client?

0 Answers  


What happens if null values are involved in comparison operations?

0 Answers  


Differnce between sel server 2000 and 2005

3 Answers  


List out some of the requirements to set up a sql server failover cluster?

0 Answers  


Categories