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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 14: Transactions and Locks<br />

If the report happened to be a long-running one, then there’s a high chance that his running it would<br />

damage the productivity of other users due to locking considerations. What’s nice about this report,<br />

though, is that it is a truly nebulous report — the exact values are probably meaningless. The manager is<br />

really just looking for ballpark numbers.<br />

By having an isolation level of READ UNCOMMITTED, we do not set any locks, so we don’t block any other<br />

transactions. Our numbers will be somewhat suspect (because of the risk of dirty reads), but we don’t<br />

need exact numbers anyway and we know that the numbers are still going to be close, even on the off<br />

chance that a dirty read is rolled back.<br />

You can get the same effect as READ UNCOMMITTED by adding the NOLOCK optimizer hint in your query.<br />

The advantage to setting the isolation level is that you don’t have to use a hint for every table in your<br />

query or use it in multiple queries. The advantage to using the NOLOCK optimizer hint is that you<br />

don’t need to remember to set the isolation level back to the default for the connection. (With READ<br />

UNCOMMITTED you do.)<br />

REPEATABLE READ<br />

The REPEATABLE READ escalates your isolation level somewhat and provides an extra level of concurrency<br />

protection by preventing not only dirty reads (the default already does that), but also preventing<br />

non-repeatable reads.<br />

That prevention of non-repeatable reads is a big upside, but holding even shared locks until the end of<br />

the transaction can block users’ access to objects, and therefore hurt productivity. Personally, I prefer to<br />

use other data integrity options (such as a CHECK constraint together with error handling) rather than<br />

this choice, but it remains an available option.<br />

The equivalent optimizer hint for the REPEATABLE READ isolation level is REPEATABLEREAD (these are<br />

the same, only no space).<br />

SERIALIZABLE<br />

444<br />

SERIALIZABLE is something of the fortress of isolation levels. It prevents all forms of concurrency issues<br />

except for a lost update. Even phantoms are prevented.<br />

When you set your isolation to SERIALIZABLE, you’re saying that any UPDATE, DELETE, or INSERT to<br />

the table or tables used by your transaction must not meet the WHERE clause of any statement in that<br />

transaction. Essentially, if the user was going to do something that your transaction would be interested<br />

in, then it must wait until your transaction has been completed.<br />

The SERIALIZABLE isolation level can also be simulated by using the SERIALIZABLE or HOLDLOCK optimizer<br />

hint in your query. Again, like the READ UNCOMMITTED and NOLOCK debate, the option of not having<br />

to set it every time versus not having to remember to change the isolation level back is the big issue.<br />

Going with an isolation level of SERIALIZABLE would, on the surface, appear to be the way you want<br />

to do everything. Indeed, it does provide your database with the highest level of what is called consistency<br />

— that is, the update process works the same for multiple users as it would if all your users did<br />

one transaction at a time (processed things serially).

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

Saved successfully!

Ooh no, something went wrong!