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.

purchased. Process 2 records sales; it updates the total amount of products sold in the Suppliers table,<br />

then decreases the inventory quantity in Products.<br />

If we run these two processes at the same time, we’re begging for trouble. Process 1 will grab an exclusive<br />

lock on the Products table. Process 2 will grab an exclusive lock on the Suppliers table. Process 1<br />

then attempts to grab a lock on the Suppliers table, but it will be forced to wait for Process 2 to clear its<br />

existing lock. In the meantime, Process 2 tries to create a lock on the Products table, but it will have to<br />

wait for Process 1 to clear its existing lock. We now have a paradox — both processes are waiting on<br />

each other. <strong>SQL</strong> <strong>Server</strong> will have to pick a deadlock victim.<br />

Now let’s rearrange that scenario, with Process 2 changed to first decrease the inventory quantity in<br />

Products, then update the total amount of products sold in the Suppliers table. This is a functional<br />

equivalent to the first way we organized the processes, and it will cost us nothing to perform it this new<br />

way. The impact though, will be stunning — no more deadlocks (at least not between these two<br />

processes)! Let’s walk through what will now happen.<br />

When we run these two processes at the same time, Process 1 will grab an exclusive lock on the Products<br />

table (so far, it’s the same). Process 2 will then also try to grab a lock on the Products table, but<br />

will be forced to wait for Process 1 to finish (notice that we haven’t done anything with Suppliers yet).<br />

Process 1 finishes with the Products table, but doesn’t release the lock because the transaction isn’t<br />

complete yet. Process 2 is still waiting for the lock on Products to clear. Process 1 now moves on to grab<br />

a lock on the Suppliers table. Process 2 continues to wait for the lock to clear on Products. Process 1<br />

finishes and commits or rolls back the transaction as required, but frees all locks in either case. Process 2<br />

now is able to obtain its lock on the Products table and moves through the rest of its transaction without<br />

further incident.<br />

Just swapping the order in which these two queries are run has eliminated a potential deadlock problem. Keep<br />

things in the same order wherever possible and you, too, will experience far less in the way of deadlocks.<br />

Keeping Transactions as Short as Possible<br />

This is another of the basics. Again, it should become just an instinct — something you don’t really think<br />

about, something you just do.<br />

This is one that never has to cost you anything really. Put what you need to put in the transaction and<br />

keep everything else out — it’s just that simple. Why this works isn’t rocket science — the longer the<br />

transaction is open and the more it touches (within the transaction), the higher the likelihood that you’re<br />

going to run into some other process that wants one or more of the objects that you’re using (reducing<br />

concurrency). If you keep your transaction short, you minimize the number of objects that can potentially<br />

cause a deadlock, plus you cut down on the time that you have your lock on them. It’s as simple as that.<br />

Keeping transactions in one batch minimizes network roundtrips during a transaction, reducing possible<br />

delays in completing the transaction and releasing locks.<br />

Use the Lowest Transaction Isolation Level Possible<br />

Chapter 14: Transactions and Locks<br />

This one is considerably less basic and requires some serious thought. As such, it isn’t surprising just<br />

how often it isn’t thought of at all. Consider it Rob’s axiom — that which requires thought is likely not to<br />

be thought of. Be different: Think about it.<br />

447

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

Saved successfully!

Ooh no, something went wrong!