DelphiFAQ Home Search:

How can I simulate a deadlock for testing purposes?

 

comments5 comments. Current rating: 5 stars (1 votes). Leave comments and/ or rate it.

Question:

How can I simulate a deadlock for testing purposes?

Answer:

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

Content-type: text/html

Comments:

2008-03-18, 02:01:16
anonymous from Iran  
thanks for your advices,i should try it,then i say my poinion.
2009-12-08, 00:14:19   (updated: 2009-12-08, 00:15:08)
anonymous from India  
Throwing a Deadlock exception from a C++ code
throw (SAException((SAErrorClass_t)2,1205,1205,(SAString)'Deadlock'));

2012-11-12, 03:39:48
coach001 from United Kingdom  
2012-11-18, 21:20:31
vfbgb  
2013-08-02, 00:56:46
anonymous from Singapore  
rating
Thanks

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: