 




<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: How to push the Database into deadlock state?</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/itanswers/how-to-push-the-database-into-deadlock-state/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/itanswers/how-to-push-the-database-into-deadlock-state/</link>
	<description></description>
	<lastBuildDate>Tue, 21 May 2013 08:51:17 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>By: saimadhu</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/how-to-push-the-database-into-deadlock-state/#comment-58241</link>
		<dc:creator>saimadhu</dc:creator>
		<pubDate>Tue, 02 Dec 2008 03:51:38 +0000</pubDate>
		<guid isPermaLink="false">#comment-58241</guid>
		<description><![CDATA[Thanks Kccrosser  for detailed info.]]></description>
		<content:encoded><![CDATA[<p>Thanks Kccrosser  for detailed info.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kccrosser</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/how-to-push-the-database-into-deadlock-state/#comment-58240</link>
		<dc:creator>kccrosser</dc:creator>
		<pubDate>Tue, 02 Dec 2008 03:37:28 +0000</pubDate>
		<guid isPermaLink="false">#comment-58240</guid>
		<description><![CDATA[Any database can be placed into a deadlock (at least temporarily) by the following:

Thread A:
    select ... from tableA for update...   (succeeds)

Thread B:
   select ... from tableB for update...   (succeeds)
   select ... from tableA for update...   (blocks on Thread A)

Thread A:
   select ... from tableB for update ...  (blocks on Thread B - deadlock)

However, most current databases are quite good at detecting these conditions, and will usually quickly terminate one of the threads.  Typically, it will kill the thread which first &quot;detected&quot; the deadlock - A in this case.

You may be able to configure the database deadlock detection timeout to extend for a while, to allow you to fail the database while in this condition.  Otherwise, you may need to generate a set of transactions with a series of tables, to try to defeat the deadlock detection algorithms.  The obvious way is just to use a series of similar tables, A, B, C, D, ... and threads, such that each thread uses the &quot;next&quot; pair of tables, and the &quot;last&quot; thread uses the last table and table A.  For example, a 4 table version would be:
Thread A:
   select...  from A for update
   select ... from B for update
Thread B:
   select ... from B for update
   select ... from C for update
Thread C:
   select ... from C for update
   select ... from D for update
Thread D:
   select ... from D for update
   select ... from A for update

(Observant developers will immediately think about doing this with a single procedure that takes a starting table name [or pair of table names] and can be queued/scheduled to generate any number of threads.)

Note that you will need to sequence these, such that the second Select for Thread A doesn&#039;t run until the first Select from Thread B succeeds, etc. - each of these Threads must succeed on the first table in order for this to create a nice cyclic lock.

Depending on your database, this can be done in a variety of ways.  I would probably create another thread that simply updates yet another table with a &quot;sequence&quot; value, timed to occur at some interval (e.g., 1 second), and then have a simple loop in each of these that watched for the appropriate sequence value to procede, e.g.:

   while vSeq &lt;&gt; [myvalue]
      sleep [some amount];
      select nextcode from seqtable into vSeq;
   wend;
(obviously, use your appropriate DB syntax here)

Alternately, if you can control the starts of these processes, they can just wait for specific times to allow the next thread to get started and then proceed.

Starting the threads is also DB dependent, but generally trivial - in Oracle, I would probably just use the jobs package (making sure I had more concurrent job queues than the number of threads I needed!).

Note that you will probably need to extend the default time to wait on a table lock, as many databases will now kill a thread that waits on a table lock for too long (which may be just seconds).

All in all, a fun little exercise...]]></description>
		<content:encoded><![CDATA[<p>Any database can be placed into a deadlock (at least temporarily) by the following:</p>
<p>Thread A:<br />
    select &#8230; from tableA for update&#8230;   (succeeds)</p>
<p>Thread B:<br />
   select &#8230; from tableB for update&#8230;   (succeeds)<br />
   select &#8230; from tableA for update&#8230;   (blocks on Thread A)</p>
<p>Thread A:<br />
   select &#8230; from tableB for update &#8230;  (blocks on Thread B &#8211; deadlock)</p>
<p>However, most current databases are quite good at detecting these conditions, and will usually quickly terminate one of the threads.  Typically, it will kill the thread which first &#8220;detected&#8221; the deadlock &#8211; A in this case.</p>
<p>You may be able to configure the database deadlock detection timeout to extend for a while, to allow you to fail the database while in this condition.  Otherwise, you may need to generate a set of transactions with a series of tables, to try to defeat the deadlock detection algorithms.  The obvious way is just to use a series of similar tables, A, B, C, D, &#8230; and threads, such that each thread uses the &#8220;next&#8221; pair of tables, and the &#8220;last&#8221; thread uses the last table and table A.  For example, a 4 table version would be:<br />
Thread A:<br />
   select&#8230;  from A for update<br />
   select &#8230; from B for update<br />
Thread B:<br />
   select &#8230; from B for update<br />
   select &#8230; from C for update<br />
Thread C:<br />
   select &#8230; from C for update<br />
   select &#8230; from D for update<br />
Thread D:<br />
   select &#8230; from D for update<br />
   select &#8230; from A for update</p>
<p>(Observant developers will immediately think about doing this with a single procedure that takes a starting table name [or pair of table names] and can be queued/scheduled to generate any number of threads.)</p>
<p>Note that you will need to sequence these, such that the second Select for Thread A doesn&#8217;t run until the first Select from Thread B succeeds, etc. &#8211; each of these Threads must succeed on the first table in order for this to create a nice cyclic lock.</p>
<p>Depending on your database, this can be done in a variety of ways.  I would probably create another thread that simply updates yet another table with a &#8220;sequence&#8221; value, timed to occur at some interval (e.g., 1 second), and then have a simple loop in each of these that watched for the appropriate sequence value to procede, e.g.:</p>
<p>   while vSeq &lt;&gt; [myvalue]<br />
      sleep [some amount];<br />
      select nextcode from seqtable into vSeq;<br />
   wend;<br />
(obviously, use your appropriate DB syntax here)</p>
<p>Alternately, if you can control the starts of these processes, they can just wait for specific times to allow the next thread to get started and then proceed.</p>
<p>Starting the threads is also DB dependent, but generally trivial &#8211; in Oracle, I would probably just use the jobs package (making sure I had more concurrent job queues than the number of threads I needed!).</p>
<p>Note that you will probably need to extend the default time to wait on a table lock, as many databases will now kill a thread that waits on a table lock for too long (which may be just seconds).</p>
<p>All in all, a fun little exercise&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using memcached
Database Caching 6/9 queries in 0.014 seconds using memcached
Object Caching 282/285 objects using memcached

Served from: itknowledgeexchange.techtarget.com @ 2013-05-21 08:58:57 -->