Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Authors

Search

XML Feeds

Google Ads

« Do not use the float data typeBeginning stages of a DR plan for SQL Server »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

I believe I have discovered a deadlock situation that SQL Server is not able to detect, so a perpetual block occurs.

A deadlock is nothing more than mutual blocking. Blocking is when a process is forced to wait for a resource while another process exclusively accesses it (where the exclusivity is managed through locks). Mutual blocking is when both processes have a lock on a resource the other is asking for. Neither can proceed, but neither will release its lock.

Normally, when SQL server detects this kind of mutual block, it picks one process as the "victim" and kills it, rolling back its work. This also releases any locks it had and allows the other process to acquire its previously blocked request, resolving the deadlock.

But what if the two resources in question are on separate servers? For SQL Server to detect deadlocks, it has to have enough information to see that a pattern of blocks is mutual.

  1. --Query 1
  2. UPDATE B
  3. SET B.Name = A.Name
  4. FROM
  5.    TableA A
  6.    INNER JOIN LinkedServer.DB1.dbo.TableB B ON A.ID = B.ID
  7.  
  8. --Query 2
  9. UPDATE B
  10. SET B.Name = A.Name
  11. FROM
  12.    LinkedServer.DB1.dbo.TableB B
  13.    INNER JOIN TableA A ON B.ID = A.ID

Now, let's say that locks for these two queries are granted in this order: query 1 acquires a lock on TableA before Table B, but query 2 acquires a lock on TableB before TableA. Even though the example I'm giving here may not be the greatest, the possibility is not so unlikely, and I'm sure there are plenty of situations out in the wild where my suggested scenario is possible.

Locks are not granted all at once, nor can one assume that the final lock used for an update is granted first. Many times a less exclusive lock is acquired and then the lock's exclusiveness is increased or specificity is broadened, and that's all that's required to cause a deadlock. See Deadlocks with Custom Sequence for one example of surprising deadlocks occurring because of an unusual transaction isolation level.

Given the above hypothetical lock order, when each query's process requests a lock on the other table to perform its update, it will be blocked. But each server involved only has half the picture and can only see a simple block, without knowing it's mutual (because the other half is occurring on the other side of the linked server). So it is a perpetual mutual block and no deadlock is detected.

The next time I am working heavily with linked servers you can be sure I'll be thinking about this and coming up with a way to test if such a "distributed deadlock" really can occur, and if so, what to do about it.

In closing, I confess that it's possible that DTC could have provisions for sharing lock information, so perhaps a distributed deadlock will be properly detected. But I have my doubts about that.

About the Author

Erik has been working in IT for 15 years and since 2004 has specialized in MS SQL Server query writing, database design, and reporting services. He also professionally does web site design and writes C# applications (mostly for projects involving his databases and web sites). His career in the industry truly started when he first began using computers in the late 70s. In 1984 he began programming on the IBM PCJr, and performed game testing on King's Quest I. These early experiences gave him the taste and drive to continue an incremental and self-taught path all the way to his current position. Erik is also interested in taekwondo, go (ranks 4k on KGS), sci-fi books, mathematics, philosophy, religion, and rollerblading. He lives with his family on the West Coast of the US.
Social SitingsLTD RSS Feed
266 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

1 comment

Comment from: SQLDenis [Member] Email
This is really interesting...I have never seen this (because mostly I don't have code like that across linked servers) and it will be nice if someone who had this happen would leave you a comment
11/13/09 @ 18:04

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)