<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>When acquiring locks in the same order is not possible or not feasible.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/01/15/when-acquiring-locks-in-the-same-order-is-not-possible-or-not-feasible.aspx</link><description>To avoid deadlocks, one of the most common recommendations is "to acquire locks in the same order" or "access objects in the same order". Clearly this makes perfect sense, but is it always feasible? Is it always possible? I keep encountering cases when</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: When acquiring locks in the same order is not possible or not feasible.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/01/15/when-acquiring-locks-in-the-same-order-is-not-possible-or-not-feasible.aspx#21099</link><pubDate>Fri, 15 Jan 2010 23:17:13 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:21099</guid><dc:creator>merrillaldrich</dc:creator><description>&lt;p&gt;I've been able to work around this in some tough cases by adding a select statement with a locking hint upstream, to force the order. (this violates your rule about no hints, though :-) For example, if it's important to lock a row in table A and then delete from table B, and then delete from A, it's possible to select from A with locking hints to &amp;quot;reserve&amp;quot; the parent row, then delete children from B, then the parent from A. A little ugly, but it works as a last resort...&lt;/p&gt;
</description></item><item><title>re: When acquiring locks in the same order is not possible or not feasible.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/01/15/when-acquiring-locks-in-the-same-order-is-not-possible-or-not-feasible.aspx#21107</link><pubDate>Sat, 16 Jan 2010 14:26:28 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:21107</guid><dc:creator>Alex K</dc:creator><description>&lt;p&gt;Merril,&lt;/p&gt;
&lt;p&gt;In the scenario which you are describing, you do not need to acquire locks on all objects - locks on just one, on the parent row, is enough. If I understood you correctly, that is exactly what you are doing: getting an exclusive lock on one resource. As long as you are acquiring that one exclusive lock in the same way in all your modules, you are fine, and your approach is equivalent to sp_getapplock. If, however, you are using different commands to get it, you still may get deadlocks.&lt;/p&gt;
&lt;p&gt;This is the whole point of my post: instead of trying to maintain the same order, serialize on a single resource.&lt;/p&gt;
</description></item><item><title>re: When acquiring locks in the same order is not possible or not feasible.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/01/15/when-acquiring-locks-in-the-same-order-is-not-possible-or-not-feasible.aspx#21217</link><pubDate>Tue, 19 Jan 2010 21:57:56 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:21217</guid><dc:creator>Armando Prato</dc:creator><description>&lt;p&gt;Alex, have you used the technique of capturing the 1205 message and re-trying the transaction? &amp;nbsp;Admittedly, I have never had the need for it but was curious if you have and if you've found the results less than ideal.&lt;/p&gt;
</description></item><item><title>re: When acquiring locks in the same order is not possible or not feasible.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/01/15/when-acquiring-locks-in-the-same-order-is-not-possible-or-not-feasible.aspx#21218</link><pubDate>Tue, 19 Jan 2010 22:20:14 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:21218</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;Armando,&lt;/p&gt;
&lt;p&gt;If I catch 1205 and retry, it definitely works. If I do it in C#, I can do it just once for all modifications, so there is very little effort involved. However, automatically retrying after deadlocks may lead to lost updates, and that might be not good:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/01/08/retrying-after-deadlocks-leads-to-lost-updates.aspx"&gt;http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/01/08/retrying-after-deadlocks-leads-to-lost-updates.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: When acquiring locks in the same order is not possible or not feasible.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/01/15/when-acquiring-locks-in-the-same-order-is-not-possible-or-not-feasible.aspx#21278</link><pubDate>Wed, 20 Jan 2010 19:57:09 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:21278</guid><dc:creator>Armando Prato</dc:creator><description>&lt;p&gt;Right, I've read that one before. &amp;nbsp;I guess my fundamental question is whether a mutex lock really makes sense or should you just make an attempt at re-trying at least once before giving up. &amp;nbsp; The way I look at it, if you fail on a 2nd attempt or subsequent attempts, you likely have a larger issue somewhere else.&lt;/p&gt;
</description></item><item><title>re: When acquiring locks in the same order is not possible or not feasible.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2010/01/15/when-acquiring-locks-in-the-same-order-is-not-possible-or-not-feasible.aspx#21281</link><pubDate>Wed, 20 Jan 2010 20:16:21 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:21281</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>&lt;p&gt;Armando,&lt;/p&gt;
&lt;p&gt;In my benchmarks, custom tailored for my environment, it is much faster to avoid deadlocks than to have them and retry. I cannot make blanket statements other than that we need to benchmark if performance is important.&lt;/p&gt;
</description></item></channel></rss>