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.

CHAPTER 6<br />

■ ■ ■<br />

Locking <strong>and</strong> Latching<br />

One of the key challenges in developing multiuser, database-driven applications is to maximize<br />

concurrent access <strong>and</strong>, at the same time, ensure that each user is able to read <strong>and</strong><br />

modify the data in a consistent fashion. The locking mechanisms that allow this to happen are<br />

key features of any database, <strong>and</strong> <strong>Oracle</strong> excels in providing them. However, <strong>Oracle</strong>’s implementation<br />

of these features is specific to <strong>Oracle</strong>—just as SQL Server’s implementation is to<br />

SQL Server—<strong>and</strong> it is up to you, the application developer, to ensure that when your application<br />

performs data manipulation, it uses these mechanisms correctly. If you fail to do so, your<br />

application will behave in an unexpected way, <strong>and</strong> inevitably the integrity of your data will be<br />

compromised (as was demonstrated in Chapter 1).<br />

In this chapter, we’ll take a detailed look at how <strong>Oracle</strong> locks both data (e.g., rows in tables)<br />

<strong>and</strong> shared data structures (such as those found in the SGA). We’ll investigate the granularity<br />

to which <strong>Oracle</strong> locks data <strong>and</strong> what that means to you, the developer. When appropriate, I’ll<br />

contrast <strong>Oracle</strong>’s locking scheme with other popular implementations, mostly to dispel the<br />

myth that row-level locking adds overhead; it adds overhead only if the implementation adds<br />

overhead. In the next chapter, we’ll continue this discussion <strong>and</strong> investigate <strong>Oracle</strong>’s multiversioning<br />

techniques <strong>and</strong> how locking strategies interact with them.<br />

What Are Locks?<br />

Locks are mechanisms used to regulate concurrent access to a shared resource. Note how I<br />

used the term “shared resource” <strong>and</strong> not “database row.” It is true that <strong>Oracle</strong> locks table data<br />

at the row level, but it also uses locks at many other levels to provide concurrent access to<br />

various resources. For example, while a stored procedure is executing, the procedure itself is<br />

locked in a mode that allows others to execute it, but it will not permit another user to alter<br />

it in any way. Locks are used in the database to permit concurrent access to these shared<br />

resources, while at the same time providing data integrity <strong>and</strong> consistency.<br />

In a single-user database, locks are not necessary. There is, by definition, only one user<br />

modifying the information. However, when multiple users are accessing <strong>and</strong> modifying data<br />

or data structures, it is crucial to have a mechanism in place to prevent concurrent modification<br />

of the same piece of information. This is what locking is all about.<br />

It is very important to underst<strong>and</strong> that there are as many ways to implement locking in a<br />

database as there are databases. Just because you have experience with the locking model of<br />

one particular relational database management system (RDBMS) does not mean you know<br />

everything about locking. For example, before I got heavily involved with <strong>Oracle</strong>, I used other<br />

databases such as Sybase, Microsoft SQL Server, <strong>and</strong> Informix. All three of these databases<br />

183

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

Saved successfully!

Ooh no, something went wrong!