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 />

We have several different transaction isolation levels available. The default is READ COMMITTED. Using a<br />

lower isolation level holds shared locks for a shorter duration than using a higher isolation level does,<br />

thereby reducing locking contention.<br />

No Open-Ended Transactions<br />

This is probably the most commonsense of all the recommendations here, but it’s one that’s often violated<br />

because of past practices.<br />

One of the ways we used to prevent lost updates (mainframe days here, folks!) was just to grab the lock<br />

and hold it until we were done with it. I can’t tell you how problematic this was (can you say yuck?).<br />

Imagine this scenario (it’s a real-life example): Someone in your service department likes to use update<br />

screens (exclusive locks) instead of display screens (shared locks) to look at data. He goes on to look at a<br />

work order. Now his buddy calls and asks if he’s ready for lunch. “Sure!” comes the reply, and the service<br />

clerk heads off to a rather long lunch (one to two hours). Everyone who is interested in this record is<br />

now locked out of it for the duration of this clerk’s lunch.<br />

Wait — it gets worse. In the days of the mainframe, you used to see the concept of queuing far more<br />

often (it actually can be quite efficient). Now someone submits a print job (which is queued) for this<br />

work order. It sits in the queue waiting for the record lock to clear. Since it’s a queue environment, every<br />

print job your company has for work orders now piles up behind that first print job (which is going to<br />

wait for that person’s lunch before clearing).<br />

This is a rather extreme example, but I’m hoping that it clearly illustrates the point. Don’t ever create<br />

locks that will still be open when you begin some form of open-ended process. Usually we’re talking<br />

user interaction (like our lunch lover), but it could be any process that has an open-ended wait to it.<br />

Summary<br />

448<br />

Transactions and locks are both cornerstones of how <strong>SQL</strong> <strong>Server</strong> works and, therefore, maximizing your<br />

development of solutions in <strong>SQL</strong> <strong>Server</strong>.<br />

By using transactions, you can make sure that everything you need to have happen as a unit happens, or<br />

none of it does. <strong>SQL</strong> <strong>Server</strong>’s use of locks ensures that we avoid the pitfalls of concurrency to the maximum<br />

extent possible (you’ll never avoid them entirely, but it’s amazing how close you can come with a<br />

little — OK, a lot — of planning). By using the two together, you are able to pass what the database<br />

industry calls the ACID test. If a transaction is ACID, then it has:<br />

❑ Atomicity: The transaction is all or nothing.<br />

❑ Consistency: All constraints and other data integrity rules have been adhered to, and all related<br />

objects (data pages, index pages) have been updated completely.<br />

❑ Isolation: Each transaction is completely isolated from any other transaction. The actions of one<br />

transaction cannot be interfered with by the actions of a separate transaction.<br />

❑ Durability: After a transaction is completed, its effects are permanently in place in the system.<br />

The data is safe, in the sense that things such as a power outage or other non-disk system failure<br />

will not lead to data that is only half written.

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

Saved successfully!

Ooh no, something went wrong!