20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11<br />

In order to find and fix such situations, most DBMSs periodically scan for deadlocks by checking all the<br />

locks held by the active transactions. If it detects a deadlock, the DBMS simply picks one transaction at<br />

random and rolls it back, thereby releasing the locks it applied. That transaction is considered the “loser”<br />

of the deadlock since the work already applied for that transaction is undone and must be rerun. The<br />

other user can now gain control of the data they need to finish their transaction and release their locks.<br />

The losing transaction receives an error message informing it of the deadlock loss and the ensuing rollback.<br />

Of course, this is a highly simplified scenario. It is not only possible but also likely that a DBMS<br />

sees deadlocks involving several different users, necessitating multiple rollbacks so that a single transaction<br />

can proceed and release its locks.<br />

This method of breaking deadlocks means that any transaction could potentially be rolled back even if<br />

all of its <strong>SQL</strong> statements are perfectly valid and would otherwise process without error. Any statement,<br />

even a simple SELECT statement, could be a deadlock loser and be rolled back. The statement is rolled<br />

back not because of any fault of its own, but simply because it is involved in a deadlock and loses the<br />

coin toss for which transaction gets rolled back. This is a small price to pay given the alternatives: a<br />

deadlock or a database corruption.<br />

If the deadlock loser is an actual person in an interactive program, the user can simply reenter the <strong>SQL</strong><br />

statements. In any program using <strong>SQL</strong>, the program must be prepared to handle the error codes that the<br />

DBMS returns informing the code of the deadlock loss and consequent rollback. Typically, just as would<br />

happen with a live user, the program simply resubmits the transaction.<br />

In addition to lock levels, the next section discusses how you can also set parameters to your locks.<br />

Setting Lock Parameters<br />

Over time, the DBMSs from various vendors have evolved capabilities not necessarily provided in the<br />

original <strong>SQL</strong> specifications. These capabilities may or may not be present in a given platform, and even<br />

if present, they may act differently. Locks affect many different operations, and efficient locking is an<br />

area that every vendor takes seriously. The vendors have provided the DBMS administrator various<br />

parameters to use in adjusting the database, which are covered here.<br />

Lock Size<br />

As discussed earlier, most modern databases can lock data at various levels of granularity; thus a DBMS<br />

may offer database-, table-, page-, and row-level locking.<br />

Number of Locks<br />

The DBMS typically has a lock pool with a finite number of locks available to all the transactions concurrently<br />

processing. Thus, the total number of locks is distributed to and recovered from transactions as<br />

the transactions begin and end. The administrator may need to adjust the total number of locks to allow<br />

for very complex transactions or very large numbers of transactions. Likewise, limiting the number of<br />

locks can force the DBMS to escalate locking earlier, increasing the efficiency of the database.<br />

Escalation<br />

316<br />

The process of requesting locks, checking the locks against requests for data by other users, and other<br />

such overhead can be very time-consuming. In an effort to minimize the overhead, the database may<br />

escalate a set of locks. Escalation refers to the database’s ability to consolidate many locks at one level of<br />

granularity into a single lock at a higher granularity. For example, if a transaction applies for hundreds

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

Saved successfully!

Ooh no, something went wrong!