20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

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.

Shared<br />

A shared lock essentially means that no one can modify the data, but other users may look at and use the<br />

data for any purpose not requiring an update. A user may need to run a report that asks different questions<br />

about a set of records. If any of those records were to be updated by another user, then the questions<br />

would not provide consistent answers since the data provided at the end would not be the same as<br />

the data at the beginning. On the other hand, it is perfectly valid for other users to ask questions about<br />

that same set of data. It is not necessary to lock the data such that no one else can see it.<br />

Exclusive<br />

An exclusive lock means that other users should not be allowed to see the data because the data is about<br />

to be changed. If one user is updating the price of every inventory item by 5 percent and another user is<br />

running a report about the value of all the items in the inventory, the report would return a different<br />

answer depending on when the report started running and how many items had already been updated.<br />

Deadlocks<br />

If you think about users locking different records in different tables, it becomes obvious that the database<br />

could get in a situation where user 1 is waiting for data locked by user 2, and user 2 is waiting for data<br />

locked by user 1. Each user needs the data locked by the other user before they can finish their process<br />

and release their locks. This situation is known as a deadlock. Deadlocks inevitably occur, and the DBMS<br />

must have a means to identify a deadlock and then must have a strategy for breaking the deadlock and<br />

allowing one of the users to gain control of the data they need in order to release the locks they applied.<br />

Unless the DBMS intervenes, both user requests wait forever for the other to unlock the necessary data.<br />

This is a simple two-way deadlock, but of course deadlocks can occur between many different users at<br />

once. Figure 11-7 shows a situation where two different transactions have locks applied, and each is waiting<br />

for the data locked by the other transaction in order to continue. Transaction A has locked record 2 in<br />

the Products table, and Transaction B is waiting to use record 2 in the Products table. Transaction B has<br />

locked record 79 in the Orders table, and Transaction A is waiting for record 79 in the Orders table.<br />

Neither transaction can continue until it gains control of the record locked by the other transaction.<br />

Transaction<br />

A<br />

Locks<br />

Record 2<br />

Waiting<br />

for rec<br />

79 in order<br />

to finish<br />

Figure 11-7<br />

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

78<br />

79<br />

80<br />

81<br />

82<br />

83<br />

84<br />

85<br />

Products<br />

Orders<br />

Transaction<br />

B<br />

Waiting for<br />

rec 2<br />

in order to<br />

finish<br />

Locks<br />

Record 79<br />

Transactions<br />

315

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

Saved successfully!

Ooh no, something went wrong!