17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 14: Transactions and Locks<br />

How <strong>SQL</strong> <strong>Server</strong> Figures Out There’s a Deadlock<br />

Every five seconds <strong>SQL</strong> <strong>Server</strong> checks all the current transactions for what locks they are waiting on, but<br />

haven’t yet been granted. As it does this, it essentially makes a note that the request exists. It will then<br />

recheck the status of all open lock requests again and, if one of the previous requests has still not been<br />

granted, it will recursively check all open transactions for a circular chain of lock requests. If it finds such<br />

a chain, then one or more deadlock victims will be chosen.<br />

How Deadlock Victims Are Chosen<br />

By default, a deadlock victim is chosen based on the cost of the transactions involved. The transaction<br />

that costs the least to rollback will be chosen (in other words, <strong>SQL</strong> <strong>Server</strong> has to do the least number of<br />

things to undo it). You can, to some degree, override this by using the DEADLOCK_PRIORITY SET option<br />

available in <strong>SQL</strong> <strong>Server</strong>; this is, however, generally both ill advised and out of the scope of this book.<br />

Avoiding Deadlocks<br />

Deadlocks can’t be avoided 100 percent of the time in complex systems, but you can almost always<br />

totally eliminate them from a practical standpoint — that is, make them so rare that they have little relevance<br />

to your system.<br />

To cut down or eliminate deadlocks, follow these simple (OK, usually simple) rules:<br />

❑ Use your objects in the same order.<br />

❑ Keep your transactions as short as possible and in one batch.<br />

❑ Use the lowest transaction isolation level necessary.<br />

❑ Do not allow open-ended interruptions (user interactions, batch separations) within the same<br />

transaction.<br />

❑ In controlled environments, use bound connections.<br />

Nearly every time I run across deadlocking problems, at least one (usually more) of these rules has been<br />

violated. Let’s look at each one individually.<br />

Use Objects in the Same Order<br />

446<br />

This is the most common problem area within the few rules that I consider to be basic. What’s great<br />

about using this rule is that it almost never costs you anything to speak of — it’s more a way of thinking.<br />

You decide early in your design process how you want to access your database objects — including<br />

order — and it becomes a habit in every query, procedure, or trigger that you write for that project.<br />

Think about it for a minute — if our problem is that our two connections each have what the other wants,<br />

then it implies that we’re dealing with the problem too late in the game. Let’s look at a simple example.<br />

Consider that we have two tables: Suppliers and Products. Now say that we have two processes that<br />

make use of both of these tables. Process 1 accepts inventory entries, updates Products with the new<br />

amount of products on hand, and then updates Suppliers with the total amount of products that we’ve

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!