Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Chapter 14: Transactions and Locks The SQL Server lock manager is that shopkeeper. When you come into the SQL Server store, the lock manager asks what your intent is — what it is you’re going to be doing. If you say “just looking,” and no one else already there is doing anything other than just looking, then the lock manager will let you in. If you want to buy (update or delete) something, then the lock manager will check to see if anyone’s already there. If so, you must wait, and everyone who comes in behind you will also wait. When you are let in to buy, no one else will be let in until you are done. By doing things this way, SQL Server is able to help us avoid a mix of different problems that can be created by concurrency issues. We will examine the possible concurrency problems and how to set a transaction isolation level that will prevent each, but for now, let’s move on to what can and cannot be locked, and what kinds of locks are available. What Problems Can Be Prevented by Locks Locks can address four major problems: ❑ Dirty reads ❑ Non-repeatable reads ❑ Phantoms ❑ Lost updates Each of these presents a separate set of problems and can be handled by a mix of solutions that usually includes proper setting of the transaction isolation level. Just to help make things useful as you look back at this chapter later, I’m going to include information on which transaction isolation level is appropriate for each of these problems. We’ll take a complete look at isolation levels shortly, but for now, let’s first make sure that we understand what each of these problems is all about. Dirty Reads 434 Dirty reads occur when a transaction reads a record that is part of another transaction that isn’t complete yet. If the first transaction completes normally, then it’s unlikely there’s a problem. But what if the transaction were rolled back? You would have information from a transaction that never happened from the database’s perspective! Let’s look at it in an example series of steps: Transaction 1 Command Transaction 2 Command BEGIN TRAN 3 Logical Database Value UPDATE col = 5 BEGIN TRAN 3 5 Uncommitted Database Value SELECT anything SELECT @var = col 3 5 5 ROLLBACK UPDATE anything SET whatever = @var 3 5 What Transaction 2 Shows

Oops — problem!!! Transaction 2 has now made use of a value that isn’t valid! If you try to go back and audit to find where this number came from, you’ll wind up with no trace and an extremely large headache. Fortunately, this scenario can’t happen if you’re using the SQL Server default for the transaction isolation level (called READ COMMITTED, which will be explained later in the section, “Setting the Isolation Level”). Non-Repeatable Reads It’s really easy to get this one mixed up with a dirty read. Don’t worry about that — it’s only terminology. Just get the concept. A non-repeatable read is caused when you read the record twice in a transaction and a separate transaction alters the data in the interim. For this one, let’s go back to our bank example. Remember that we don’t want the value of the account to go below 0 dollars: Transaction 1 Transaction 2 @Var What Transaction 1 Thinks Is in the Table BEGIN TRAN NULL 125 SELECT @Var = value FROM table BEGIN TRAN 125 125 125 UPDATE value, SET value = value — 50 IF @Var >=100 END TRAN 125 125 75 UPDATE value, SET value = value — 100 (Finish, wait for lock to clear, then continue) Chapter 14: Transactions and Locks 125 125 (waiting for lock to clear) Value in Table Again, we have a problem. Transaction 1 has pre-scanned (which can be a good practice in some instances — remember that section, “Handling Errors Before They Happen,” in Chapter 12?) to make sure that the value is valid and that the transaction can go through (there’s enough money in the account). The problem is that before the UPDATE was made, Transaction 2 beat Transaction 1 to the punch. If there isn’t any CHECK constraint on the table to prevent the negative value, then it would 75 75 125 75 Either: -25 (If there isn’t a CHECK constraint enforcing > 0) Or: Error 547 (If there is a CHECK) 435

Chapter 14: Transactions and Locks<br />

The <strong>SQL</strong> <strong>Server</strong> lock manager is that shopkeeper. When you come into the <strong>SQL</strong> <strong>Server</strong> store, the lock manager<br />

asks what your intent is — what it is you’re going to be doing. If you say “just looking,” and no one<br />

else already there is doing anything other than just looking, then the lock manager will let you in. If you<br />

want to buy (update or delete) something, then the lock manager will check to see if anyone’s already there.<br />

If so, you must wait, and everyone who comes in behind you will also wait. When you are let in to buy,<br />

no one else will be let in until you are done.<br />

By doing things this way, <strong>SQL</strong> <strong>Server</strong> is able to help us avoid a mix of different problems that can be created<br />

by concurrency issues. We will examine the possible concurrency problems and how to set a transaction<br />

isolation level that will prevent each, but for now, let’s move on to what can and cannot be locked, and<br />

what kinds of locks are available.<br />

What Problems Can Be Prevented by Locks<br />

Locks can address four major problems:<br />

❑ Dirty reads<br />

❑ Non-repeatable reads<br />

❑ Phantoms<br />

❑ Lost updates<br />

Each of these presents a separate set of problems and can be handled by a mix of solutions that usually<br />

includes proper setting of the transaction isolation level. Just to help make things useful as you look back<br />

at this chapter later, I’m going to include information on which transaction isolation level is appropriate<br />

for each of these problems. We’ll take a complete look at isolation levels shortly, but for now, let’s first<br />

make sure that we understand what each of these problems is all about.<br />

Dirty Reads<br />

434<br />

Dirty reads occur when a transaction reads a record that is part of another transaction that isn’t complete<br />

yet. If the first transaction completes normally, then it’s unlikely there’s a problem. But what if the transaction<br />

were rolled back? You would have information from a transaction that never happened from the<br />

database’s perspective!<br />

Let’s look at it in an example series of steps:<br />

Transaction 1<br />

Command<br />

Transaction 2<br />

Command<br />

BEGIN TRAN 3<br />

Logical<br />

Database<br />

Value<br />

UPDATE col = 5 BEGIN TRAN 3 5<br />

Uncommitted<br />

Database<br />

Value<br />

SELECT anything SELECT @var = col 3 5 5<br />

ROLLBACK UPDATE anything<br />

SET whatever = @var<br />

3 5<br />

What<br />

Transaction 2<br />

Shows

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

Saved successfully!

Ooh no, something went wrong!