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 />

Usually, the default isolation level is SERIALIZABLE. If you specify READ UNCOMMITTED, then the DBMS<br />

assumes READ ONLY, and you cannot specify READ WRITE. If not otherwise specified, the DBMS assumes<br />

READ WRITE. The purpose of these assumptions is twofold: to allow the DBMS to optimize its operation<br />

and to protect inexperienced programmers from suffering the problems associated with transactions,<br />

which are detailed in the next section.<br />

Versioning<br />

Versioning is a method of working around the issue of locks. The idea is that as modifications to records<br />

are performed, versions of the data are made available to other users. Instead of just locking a user out,<br />

the DBMS decides whether the other users need to see the modified or the unmodified version of the<br />

data. There may in fact be several different versions of the data. The DBMS tracks the isolation levels<br />

of the various transactions and makes decisions as to which version of the data any given transaction<br />

should be allowed to see based on its isolation level. As far as I know, of all the major DBMSs, only<br />

Oracle uses versioning.<br />

Locks are inherently restrictive in nature and limit concurrent operations on data. If one transaction has<br />

a write lock on data and another transaction has a SERIALIZABLE isolation level set, that transaction<br />

cannot even see the locked data. Thus that transaction simply halts processing until the data it needs<br />

to see is finally written. In a versioning system, as soon as the first transaction makes its modifications to<br />

the data, the second transaction may be allowed to see the modified or unmodified data, depending on<br />

what the DBMS decides is applicable to the operations the transaction is doing and its isolation level.<br />

Thus the second transaction can keep right on processing.<br />

Versioning databases can, under the right circumstances, increase the number of concurrent transactions<br />

executing in parallel. Because the transactions do not wait for locks to release, they can continue whatever<br />

they are doing as if each process had exclusive access to the data. Unfortunately, there is significant<br />

overhead in tracking whether the various processes did in fact interfere with each other, and in those<br />

cases, the system must undo the damage and start over from the point where the interference occurred.<br />

Problem Examples<br />

In order to understand some of the problems that transactions help to prevent and that isolation levels<br />

address, it is useful to discuss a well-known set of problems. These problems are so well known, in fact,<br />

that they have been given names and are routinely discussed when learning transactions. The following<br />

text identifies these problems, provides you with a scenario in which you might encounter each one, and<br />

classifies the isolation level you should set in order to prevent them from occurring. Following the discussion<br />

of the problems is a table that summarizes the problems and how the various isolation levels<br />

affect the problem.<br />

The Lost Update<br />

320<br />

The lost update problem illustrates what can occur when two programs read the same data from a table,<br />

use that data in calculations, and then try to update that data. A SERIALIZABLE isolation level helps you<br />

to prevent the lost update. The following situation gives you an idea of how the lost update can pop up<br />

in the real world.

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

Saved successfully!

Ooh no, something went wrong!