Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
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
- Page 422 and 423: Chapter 12: Stored Procedures It wo
- Page 424 and 425: Chapter 12: Stored Procedures 386 r
- Page 426 and 427: Chapter 12: Stored Procedures 388 -
- Page 428 and 429: Chapter 12: Stored Procedures Note
- Page 430 and 431: Chapter 12: Stored Procedures 392 n
- Page 432 and 433: Chapter 12: Stored Procedures All t
- Page 434 and 435: Chapter 12: Stored Procedures Sproc
- Page 436 and 437: Chapter 12: Stored Procedures When
- Page 438 and 439: Chapter 12: Stored Procedures 400 @
- Page 440 and 441: Chapter 12: Stored Procedures I’d
- Page 442 and 443: Chapter 12: Stored Procedures match
- Page 444 and 445: Chapter 12: Stored Procedures There
- Page 446 and 447: Chapter 12: Stored Procedures 408 f
- Page 449 and 450: 13 User-Defined Functions Well, her
- Page 451 and 452: types!), except for BLOBs, cursors,
- Page 453 and 454: We get back the same set as with th
- Page 455 and 456: AS RETURN (SELECT BusinessEntityID,
- Page 457 and 458: in your relational database. These
- Page 459 and 460: AS BEGIN ( EmployeeID int NOT NULL,
- Page 461 and 462: So, as you can see, we can actually
- Page 463 and 464: Despite being schema-bound, this on
- Page 465 and 466: 14 Transactions and Locks This is o
- Page 467 and 468: we are unable or do not want to com
- Page 469 and 470: Figure 14-1 Data needed Data in cac
- Page 471: Transaction 4 This transaction wasn
- Page 475 and 476: The only cure for this is setting y
- Page 477 and 478: Exclusive Locks Exclusive locks are
- Page 479 and 480: Also: ❑ The Sch-S is compatible w
- Page 481 and 482: The syntax for switching between th
- Page 483 and 484: As with most things in life, howeve
- Page 485 and 486: purchased. Process 2 records sales;
- Page 487: Chapter 14: Transactions and Locks
- Page 490 and 491: Chapter 15: Triggers the world’s
- Page 492 and 493: Chapter 15: Triggers WITH ENCRYPTIO
- Page 494 and 495: Chapter 15: Triggers FOR|AFTER The
- Page 496 and 497: Chapter 15: Triggers 458 To illustr
- Page 498 and 499: Chapter 15: Triggers 460 IF EXISTS
- Page 500 and 501: Chapter 15: Triggers ❑ Feeding de
- Page 502 and 503: Chapter 15: Triggers Trigger Firing
- Page 504 and 505: Chapter 15: Triggers Like regular t
- Page 506 and 507: Chapter 15: Triggers The COLUMNS_UP
- Page 508 and 509: Chapter 15: Triggers This is the sa
- Page 510 and 511: Chapter 15: Triggers 472 we have th
- Page 512 and 513: Chapter 16: A Brief XML Primer So,
- Page 514 and 515: Chapter 16: A Brief XML Primer Figu
- Page 516 and 517: Chapter 16: A Brief XML Primer Elem
- Page 518 and 519: Chapter 16: A Brief XML Primer ❑
- Page 520 and 521: Chapter 16: A Brief XML Primer 482
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