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

What is a synonym for manipulation?

0 Answers  


system date format is "yy-mm-dd" "select getdate()" ----> 2009-01-24 20:03:28.513 if i write "select dateadd(dd,2,getdate()) ".it returns "2009-01-26 19:59:38.340"...my question is dat could it be possible to retrive da date in da format "26 jan 2009 ...."??

8 Answers  


What is the Main Difference between ACCESS and SQL SERVER?

0 Answers   MindCracker,


What should be the fill factor for indexes created on tables? : sql server database administration

0 Answers  


Advantages and Disadvantages of Cursor?

15 Answers   Polytechnic, TCS, Zenith,






What is indexing explain it with an example?

0 Answers  


Explain syntax for dropping triggers?

0 Answers  


How you trouble shoot when any job fails

0 Answers  


difference between truncate, delete aur drop?

2 Answers  


How to optimize stored procedure optimization?

0 Answers  


Explain an automatic checkpoint

0 Answers  


What are the 10 characteristics of data quality?

0 Answers  


Categories