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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The only cure for this is setting your transaction isolation level to SERIALIZABLE, in which case any<br />

updates to the table must not fall within your WHERE clause or they will be locked out.<br />

Lost Updates<br />

Lost updates happen when one update is successfully written to the database, but is accidentally overwritten<br />

by another transaction. I can just hear you right about now, “Yikes! How could that happen?”<br />

Lost updates can happen when two transactions read an entire record, then one writes updated information<br />

back to the record and the other writes updated information back to the record. Let’s look at an example.<br />

Let’s say that you are a credit analyst for your company. You get a call that customer X has reached their<br />

credit limit and would like an extension, so you pull up their customer information to take a look. You<br />

see that they have a credit limit of $5,000 and that they appear to always pay on time.<br />

While you’re looking, Sally, another person in your credit department, pulls up customer X’s record to<br />

enter a change in the address. The record she pulls up also shows the credit limit of $5,000.<br />

At this point, you decide to go ahead and raise customer X’s credit limit to $7,500, and press Enter. The<br />

database now shows $7,500 as the credit limit for customer X.<br />

Sally now completes her update to the address, but she’s using the same edit screen that you are — that<br />

is, she updates the entire record. Remember what her screen showed as the credit limit? $5,000. Oops,<br />

the database now shows customer X with a credit limit of $5,000 again. Your update has been lost!<br />

The solution to this depends on your code somehow recognizing that another connection has updated<br />

your record between when you read the data and when you went to update it. How this recognition<br />

happens varies depending on what access method you’re using.<br />

Lockable Resources<br />

There are six lockable resources for <strong>SQL</strong> <strong>Server</strong>, and they form a hierarchy. The higher level the lock, the<br />

less granularity it has (that is, you’re choosing a higher and higher number of objects to be locked a cascading<br />

type of action just because the object that contains them has been locked). These include, in<br />

ascending order of granularity:<br />

❑ Database: The entire database is locked. This happens usually during database schema changes.<br />

❑ Table: The entire table is locked. This includes all the data-related objects associated with that<br />

table, including the actual data rows (every one of them) and all the keys in all the indexes associated<br />

with the table in question.<br />

❑ Extent: The entire extent is locked. Remember than an extent is made up of eight pages, so an<br />

extent lock means that the lock has control of the extent, the eight data or index pages in that<br />

extent, and all the rows of data in those eight pages.<br />

❑ Page: All the data or index keys on that page are locked.<br />

Chapter 14: Transactions and Locks<br />

❑ Key: There is a lock on a particular key or series of keys in an index. Other keys in the same<br />

index page may be unaffected.<br />

❑ Row or Row Identifier (RID): Although the lock is technically placed on the row identifier (an<br />

internal <strong>SQL</strong> <strong>Server</strong> construct), it essentially locks the entire row.<br />

437

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

Saved successfully!

Ooh no, something went wrong!