05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

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

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

16<br />

CHAPTER 1 ■ DEVELOPING SUCCESSFUL ORACLE APPLICATIONS<br />

These kinds of bugs are terribly difficult to track down <strong>and</strong> fix. If you only test your application<br />

in isolation <strong>and</strong> then deploy it to dozens of concurrent users, you are likely to be (painfully)<br />

exposed to an undetected concurrency issue.<br />

Over the next two sections, I’ll relate two small examples of how the lack of underst<strong>and</strong>ing<br />

concurrency control can ruin your data or inhibit performance <strong>and</strong> scalability.<br />

Implementing Locking<br />

The database uses locks to ensure that, at most, one transaction is modifying a given piece<br />

of data at any given time. Basically, locks are the mechanism that allows for concurrency—<br />

without some locking model to prevent concurrent updates to the same row, for example,<br />

multiuser access would not be possible in a database. However, if overused or used improperly,<br />

locks can actually inhibit concurrency. If you or the database itself locks data unnecessarily,<br />

then fewer people will be able to concurrently perform operations. Thus, underst<strong>and</strong>ing what<br />

locking is <strong>and</strong> how it works in your database is vital if you are to develop a scalable, correct<br />

application.<br />

What is also vital is that you underst<strong>and</strong> that each database implements locking differently.<br />

Some have page-level locking, others have row-level locking; some implementations escalate<br />

locks from row level to page level, whereas others do not; some use read locks, others do not;<br />

<strong>and</strong> some implement serializable transactions via locking <strong>and</strong> others via read-consistent views<br />

of data (no locks). These small differences can balloon into huge performance issues or downright<br />

bugs in your application if you do not underst<strong>and</strong> how they work.<br />

The following points sum up <strong>Oracle</strong>’s locking policy:<br />

• <strong>Oracle</strong> locks data at the row level on modification only. There is no lock escalation to a<br />

block or table level under normal circumstances (there is a short period of time during<br />

a two-phase commit, a not common operation, where this is not true).<br />

• <strong>Oracle</strong> never locks data just to read it. There are no locks placed on rows of data by<br />

simple reads.<br />

• A writer of data does not block a reader of data. Let me repeat: reads are not blocked by<br />

writes. This is fundamentally different from almost every other database, where reads<br />

are blocked by writes. While this sounds like an extremely positive attribute (it generally<br />

is), if you do not underst<strong>and</strong> this idea thoroughly, <strong>and</strong> you attempt to enforce integrity<br />

constraints in your application via application logic, you are most likely doing it incorrectly.<br />

We will explore this topic in Chapter 7 on concurrency control in much more<br />

detail.<br />

• A writer of data is blocked only when another writer of data has already locked the row<br />

it was going after. A reader of data never blocks a writer of data.<br />

You must take these facts into consideration when developing your application, <strong>and</strong> you<br />

must also realize that this policy is unique to <strong>Oracle</strong>—every database has subtle differences in<br />

its approach to locking. Even if you go with lowest common denominator SQL in your applications,<br />

the locking <strong>and</strong> concurrency control models employed by each database’s vendor<br />

dictates that something about how your application behaves will be different. A developer<br />

who does not underst<strong>and</strong> how his or her database h<strong>and</strong>les concurrency will certainly

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

Saved successfully!

Ooh no, something went wrong!