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.

As with most things in life, however, there is a trade-off. Consistency and concurrency can, in a practical<br />

sense, be thought of as polar opposites. Making things SERIALIZABLE can prevent other users from<br />

getting to the objects they need — that equates to lower concurrency. The reverse is also true — increasing<br />

concurrency (by going to a REPEATABLE READ, for example) reduces the consistency of your database.<br />

My personal recommendation on this is to stick with the default (READ COMMITTED) unless you have<br />

a specific reason not to.<br />

SNAPSHOT<br />

Chapter 14: Transactions and Locks<br />

SNAPSHOT is the newest of the isolation levels (it was added in <strong>SQL</strong> <strong>Server</strong> 2005) and most closely<br />

resembles a combination of the READ COMMITTED and READ UNCOMMITTED. It’s important to note<br />

that SNAPSHOT is not available by default — instead it becomes available only if a special option,<br />

ALLOW_SNAPSHOT_ISOLATION, has been activated for the database.<br />

Much like READ UNCOMMITTED, SNAPSHOT does not create any locks, nor does it generally honor them.<br />

The primary difference between the two is that they recognize changes taking place in the database at<br />

different times. Any change in the database, regardless of when or if it is committed, is seen by queries<br />

running the READ UNCOMMITTED isolation level. With SNAPSHOT only changes that were committed prior<br />

to the start of the SNAPSHOT transaction are seen. From the start of the SNAPSHOT transaction, all data is<br />

viewed exactly as it was committed at the start of the transaction.<br />

There are two special things to note relating to SNAPSHOT. First, a special database option has to be<br />

turned on before you can even use the SNAPSHOT isolation level, and that option must be on for every<br />

database included in your transaction (keep this in mind in case your queries span databases). Next,<br />

while SNAPSHOT does not generally pay attention to or set locks, there is one special instance where it<br />

will. If there is a database recovery rollback in progress when the SNAPSHOT takes place, then the SNAPSHOT<br />

transaction will set a special lock in place to serve as something of a placeholder and then wait for the<br />

rollback to complete. As soon as the rollback is complete, the lock is removed and the SNAPSHOT will<br />

move forward normally.<br />

Dealing with Deadlocks (aka “a 1205”)<br />

OK, so now you’ve seen locks, and you’ve also seen transactions. Now that you’ve got both, we can<br />

move on to the rather pesky problem of dealing with deadlocks.<br />

As we’ve already mentioned, a deadlock is not a type of lock in itself, but rather a situation where a paradox<br />

has been formed by other locks. Like it or not, you’ll bump into these on a regular basis (particularly<br />

when you’re just starting out), and you’ll be greeted with an error number 1205. So prolific is this<br />

particular problem that you’ll hear many a database developer refer to them simply by the number.<br />

Deadlocks are caused when one lock can’t do what it needs to do in order to clear, because a second lock<br />

is holding that resource, and vice versa. When this happens, somebody has to win the battle, so <strong>SQL</strong><br />

<strong>Server</strong> chooses a deadlock victim. The deadlock victim’s transaction is then rolled back and is notified<br />

that this happened through the 1205 error. The other transaction can continue normally (indeed, it will<br />

be entirely unaware that there was a problem, other than seeing an increased execution time).<br />

445

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

Saved successfully!

Ooh no, something went wrong!