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.

Exclusive Locks<br />

Exclusive locks are just what they sound like. Exclusive locks are not compatible with any other lock.<br />

They cannot be achieved if any other lock exists, nor will they allow a new lock of any form to be created<br />

on the resource while the exclusive lock is still active. This prevents two people from updating, deleting,<br />

or whatever at the same time.<br />

Update Locks<br />

Update locks are something of a hybrid between shared locks and exclusive locks. An update lock is a<br />

special kind of placeholder. Think about it — in order to do an update, you need to validate your WHERE<br />

clause (assuming there is one) to figure out just what rows you’re going to be updating. That means that<br />

you only need a shared lock, until you actually go to make the physical update. At the time of the physical<br />

update, you’ll need an exclusive lock.<br />

Update locks indicate that you have a shared lock that’s going to become an exclusive lock after you’ve<br />

done your initial scan of the data to figure out what exactly needs to be updated. This acknowledges the<br />

fact that there are two distinct stages to an update:<br />

1. The stage where you are figuring out what meets the WHERE clause criteria (what’s going to be<br />

updated). This is the part of an update query that has an update lock.<br />

2. The stage where, if you actually decide to perform the update, the lock is upgraded to an exclusive<br />

lock. Otherwise, the lock is converted to a shared lock.<br />

What’s nice about this is that it forms a barrier against one variety of deadlock. A deadlock is not a type of<br />

lock in itself, but rather a situation where a paradox has been formed. A deadlock arises if one lock can’t<br />

do what it needs to do in order to clear because another lock is holding that resource — the problem is<br />

that the opposite resource is itself stuck waiting for the lock to clear on the first transaction.<br />

Without update locks, these deadlocks would crop up all the time. Two update queries would be running<br />

in shared mode. Query A completes its query and is ready for the physical update. It wants to escalate<br />

to an exclusive lock, but it can’t because Query B is finishing its query. Query B then finishes the<br />

query, except that it needs to do the physical update. In order to do that, Query B must escalate to an<br />

exclusive lock, but it can’t because Query A is still waiting. This creates an impasse.<br />

Instead, an update lock prevents any other update locks from being established. The instant that the second<br />

transaction attempts to achieve an update lock, they will be put into a wait status for whatever the lock<br />

timeout is — the lock will not be granted. If the first lock clears before the lock timeout is reached, then the<br />

lock will be granted to the new requester and that process can continue. If not, an error will be generated.<br />

Update locks are compatible only with shared locks and intent shared locks.<br />

Intent Locks<br />

Chapter 14: Transactions and Locks<br />

An intent lock is a true placeholder and is meant to deal with the issue of object hierarchies. Imagine a situation<br />

where you have a lock established on a row, but someone wants to establish a lock on a page or<br />

extent, or modify a table. You wouldn’t want another transaction to go around yours by going higher up<br />

the hierarchy, would you?<br />

Without intent locks, the higher-level objects wouldn’t even know that you had the lock at the lower<br />

level. Intent locks improve performance, as <strong>SQL</strong> <strong>Server</strong> needs to examine intent locks only at the table<br />

439

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

Saved successfully!

Ooh no, something went wrong!