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

rekharaghuram
from rekharaghuram More from this publisher
05.11.2015 Views

CHAPTER 7 ■ ■ ■ Concurrency and Multi-Versioning As stated in the last chapter, one of the key challenges in developing multiuser, databasedriven applications is to maximize concurrent access but, at the same time, ensure that each user is able to read and modify the data in a consistent fashion. In this chapter, we’re going to take a detailed look at how Oracle achieves multi-version read consistency, and what that means to you, the developer. I will also introduce a new term, write consistency, and use it to describe how Oracle works not only in a read environment with read consistency, but also in a mixed read and write environment. What Are Concurrency Controls? Concurrency controls are the collection of functions that the database provides to allow many people to access and modify data simultaneously. As noted in the previous chapter, the lock is one of the core mechanisms by which Oracle regulates concurrent access to shared database resources and prevents “interference” between concurrent database transactions. To briefly summarize, Oracle uses a variety of locks, including the following: • TX locks: These locks are acquired for the duration of a data-modifying transaction. • TM and DDL locks: These locks ensure that the structure of an object is not altered while you are modifying its contents (TM lock) or the object itself (DDL lock). • Latches: These are internal locks that Oracle employs to mediate access to its shared data structures. In each case, there is minimal overhead associated with lock acquisition. TX transaction locks are extremely scalable in terms of both performance and cardinality. TM and DDL locks are applied in the least restrictive mode whenever possible. Latches and enqueues are both very lightweight and fast (enqueues are the slightly heavier of the two, though they’re more feature-rich). Problems only arise from poorly designed applications that hold locks for longer than necessary and cause blocking in the database. If you design your code well, Oracle’s locking mechanisms will allow for scaleable, highly concurrent applications. But Oracle’s support for concurrency goes beyond efficient locking. It implements a multi-versioning architecture (introduced in Chapter 1) that provides controlled, yet highly concurrent access to data. Multi-versioning describes Oracle’s ability to simultaneously 231

232 CHAPTER 7 ■ CONCURRENCY AND MULTI-VERSIONING materialize multiple versions of the data and is the mechanism by which Oracle provides read-consistent views of data (i.e., consistent results with respect to a point in time). A rather pleasant side effect of multi-versioning is that a reader of data will never be blocked by a writer of data. In other words, writes do not block reads. This is one of the fundamental differences between Oracle and other databases. A query that only reads information in Oracle will never be blocked, it will never deadlock with another session, and it will never get an answer that didn’t exist in the database. ■Note There is a short period of time during the processing of a distributed 2PC where Oracle will prevent read access to information. As this processing is somewhat rare and exceptional (the problem applies only to queries that start between the prepare and the commit phases and try to read the data before the commit arrives), I will not cover it in detail. Oracle’s multi-versioning model for read consistency is applied by default at the statement level (for each and every query) and can also be applied at the transaction level. This means that each and every SQL statement submitted to the database sees a read-consistent view of the database at least—and if you would like this read-consistent view of the database to be at the level of a transaction (a set of SQL statements), you may do that as well. The basic purpose of a transaction in the database is to take the database from one consistent state to the next. The ISO SQL standard specifies various transaction isolation levels, which define how “sensitive” one transaction is to changes made by another. The greater the level of sensitivity, the greater the degree of isolation the database must provide between transactions executed by your application. In the following section, we’ll look at how, via its multi-versioning architecture and with absolutely minimal locking, Oracle can support each of the defined isolation levels. Transaction Isolation Levels The ANSI/ISO SQL standard defines four levels of transaction isolation, with different possible outcomes for the same transaction scenario. That is, the same work performed in the same fashion with the same inputs may result in different answers, depending on your isolation level. These isolation levels are defined in terms of three “phenomena” that are either permitted or not at a given isolation level: • Dirty read: The meaning of this term is as bad as it sounds. You are permitted to read uncommitted, or dirty, data. You would achieve this effect by just opening an OS file that someone else is writing and reading whatever data happens to be there. Data integrity is compromised, foreign keys are violated, and unique constraints are ignored. • Nonrepeatable read: This simply means that if you read a row at time T1 and attempt to reread that row at time T2, the row may have changed. It may have disappeared, it may have been updated, and so on.

232<br />

CHAPTER 7 ■ CONCURRENCY AND MULTI-VERSIONING<br />

materialize multiple versions of the data <strong>and</strong> is the mechanism by which <strong>Oracle</strong> provides<br />

read-consistent views of data (i.e., consistent results with respect to a point in time). A rather<br />

pleasant side effect of multi-versioning is that a reader of data will never be blocked by a<br />

writer of data. In other words, writes do not block reads. This is one of the fundamental differences<br />

between <strong>Oracle</strong> <strong>and</strong> other databases. A query that only reads information in <strong>Oracle</strong> will<br />

never be blocked, it will never deadlock with another session, <strong>and</strong> it will never get an answer<br />

that didn’t exist in the database.<br />

■Note There is a short period of time during the processing of a distributed 2PC where <strong>Oracle</strong> will prevent<br />

read access to information. As this processing is somewhat rare <strong>and</strong> exceptional (the problem applies only<br />

to queries that start between the prepare <strong>and</strong> the commit phases <strong>and</strong> try to read the data before the commit<br />

arrives), I will not cover it in detail.<br />

<strong>Oracle</strong>’s multi-versioning model for read consistency is applied by default at the statement<br />

level (for each <strong>and</strong> every query) <strong>and</strong> can also be applied at the transaction level. This means<br />

that each <strong>and</strong> every SQL statement submitted to the database sees a read-consistent view of<br />

the database at least—<strong>and</strong> if you would like this read-consistent view of the database to be at<br />

the level of a transaction (a set of SQL statements), you may do that as well.<br />

The basic purpose of a transaction in the database is to take the database from one consistent<br />

state to the next. The ISO SQL st<strong>and</strong>ard specifies various transaction isolation levels,<br />

which define how “sensitive” one transaction is to changes made by another. The greater the<br />

level of sensitivity, the greater the degree of isolation the database must provide between<br />

transactions executed by your application. In the following section, we’ll look at how, via its<br />

multi-versioning architecture <strong>and</strong> with absolutely minimal locking, <strong>Oracle</strong> can support each<br />

of the defined isolation levels.<br />

Transaction Isolation Levels<br />

The ANSI/ISO SQL st<strong>and</strong>ard defines four levels of transaction isolation, with different possible<br />

outcomes for the same transaction scenario. That is, the same work performed in the same<br />

fashion with the same inputs may result in different answers, depending on your isolation<br />

level. These isolation levels are defined in terms of three “phenomena” that are either permitted<br />

or not at a given isolation level:<br />

• Dirty read: The meaning of this term is as bad as it sounds. You are permitted to read<br />

uncommitted, or dirty, data. You would achieve this effect by just opening an OS file<br />

that someone else is writing <strong>and</strong> reading whatever data happens to be there. Data<br />

integrity is compromised, foreign keys are violated, <strong>and</strong> unique constraints are ignored.<br />

• Nonrepeatable read: This simply means that if you read a row at time T1 <strong>and</strong> attempt to<br />

reread that row at time T2, the row may have changed. It may have disappeared, it may<br />

have been updated, <strong>and</strong> so on.

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

Saved successfully!

Ooh no, something went wrong!