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.

186<br />

CHAPTER 6 ■ LOCKING AND LATCHING<br />

• You should hold locks on data as long as you need to. They are tools for you to use, not<br />

things to be avoided. Locks are not a scarce resource. Conversely, you should hold locks<br />

on data only as long as you need to. Locks may not be scarce, but they can prevent<br />

other sessions from modifying information.<br />

• There is no overhead involved with row-level locking in <strong>Oracle</strong>—none. Whether you<br />

have 1 row lock or 1,000,000 row locks, the number of “resources” dedicated to locking<br />

this information will be the same. Sure, you’ll do a lot more work modifying 1,000,000<br />

rows rather than 1 row, but the number of resources needed to lock 1,000,000 rows is<br />

the same as for 1 row; it is a fixed constant.<br />

• You should never escalate a lock (e.g., use a table lock instead of row locks) because it<br />

would be “better for the system.” In <strong>Oracle</strong>, it won’t be better for the system—it will save<br />

no resources. There are times to use table locks, such as in a batch process, when you<br />

know you will update the entire table <strong>and</strong> you do not want other sessions to lock rows<br />

on you. But you are not using a table lock to make it easier for the system by avoiding<br />

having to allocate row locks.<br />

• Concurrency <strong>and</strong> consistency can be achieved simultaneously. You can get the data<br />

quickly <strong>and</strong> accurately, every time. Readers of data are not blocked by writers of data.<br />

Writers of data are not blocked by readers of data. This is one of the fundamental differences<br />

between <strong>Oracle</strong> <strong>and</strong> most other relational databases.<br />

As we cover the remaining components in this chapter <strong>and</strong> the next, I’ll reinforce these<br />

points.<br />

Locking Issues<br />

Before we discuss the various types of locks that <strong>Oracle</strong> uses, it is useful to look at some locking<br />

issues, many of which arise from badly designed applications that do not make correct use<br />

(or make no use) of the database’s locking mechanisms.<br />

Lost Updates<br />

A lost update is a classic database problem. Actually, it is a problem in all multiuser computer<br />

environments. Simply put, a lost update happens when the following events occur, in the<br />

order presented here:<br />

1. A transaction in Session1 retrieves (queries) a row of data into local memory <strong>and</strong><br />

displays it to an end user, User1.<br />

2. Another transaction in Session2 retrieves that same row, but displays the data to a<br />

different end user, User2.<br />

3. User1, using the application, modifies that row <strong>and</strong> has the application update the<br />

database <strong>and</strong> commit. Session1’s transaction is now complete.<br />

4. User2 modifies that row also, <strong>and</strong> has the application update the database <strong>and</strong><br />

commit. Session2’s transaction is now complete.

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

Saved successfully!

Ooh no, something went wrong!