< Day Day Up > |
Before you learn about the various types of locks that can be acquired by DB2, you should understand lock duration , which refers to the length of time that a lock is maintained . The duration of a lock is based on the BIND options chosen for the program requesting locks. Locks can be acquired either immediately when the plan is requested to be run or iteratively as needed during the execution of the program. Locks can be released when the plan is terminated or when they are no longer required for a unit of work. The BIND parameters affecting DB2 locking are covered in detail in Chapter 13, "Program Preparation." They are summarized in the following sections as a reminder. BIND Parameters Affecting Table Space LocksACQUIRE(ALLOCATE) versus ACQUIRE(USE) : The ALLOCATE option specifies that locks will be acquired when the plan is allocated, which normally occurs when the first SQL statement is issued. The USE option indicates that locks will be acquired only as they are required, SQL statement by SQL statement. RELEASE(DEALLOCATE) versus RELEASE(COMMIT) : When you specify DEALLOCATE for a plan, locks are not released until the plan is terminated. When you specify COMMIT , table space locks are released when a COMMIT is issued. BIND Parameters Affecting Page and Row LocksISOLATION level ( CS , RR , RS , UR ): There are four choices for isolation level.
Regardless of the ISOLATION level chosen, all page locks are released when a COMMIT is encountered . Implementing Dirty Reads Using ISOLATION(UR)Programs that read DB2 data typically access numerous rows during their execution and are thus quite susceptible to concurrency problems. DB2 provides read-through locks, also known as "dirty reads" or "uncommitted reads," to help overcome concurrency problems. When using uncommitted reads, an application program can read data that has been changed but is not yet committed. Dirty read capability is implemented using the UR isolation level ( UR stands for uncommitted read). When an application program uses the UR isolation level, it reads data without taking locks. This way, the application program can read data contained in the table as it is being manipulated. How does "dirty read" impact data availability and integrity? Consider the following sequence of events:
If the UR isolation level were specified for the second transaction, it would read the changed data even though it had yet to be committed. Obviously, if the program does not wait to take a lock and merely reads the data in whatever state it happens to be at that moment, the program will execute faster than if it has to wait for locks to be taken and resources to be freed before processing. However, you must carefully examine the implications of reading uncommitted data before implementing such a plan. Several types of "problems" can occur. A dirty read can cause duplicate rows to be returned where none exist. Also, a dirty read can cause no rows to be returned when one (or more) actually exists. Dirty reads might even cause rows to be returned out of order even when using an ORDER BY . Obviously, you must take these "problems" into consideration before using the UR isolation level. ISOLATION(UR) RequirementsThe UR isolation level applies to read-only operations: SELECT , SELECT INTO , and FETCH from a read-only result table. Any application plan or package bound with an isolation level of UR uses uncommitted read functionality for read-only SQL. Operations that are contained in the same plan or package that are not read-only use an isolation level of CS . You can override the isolation level that is defined at the plan or package level during BIND or REBIND as you want for each SQL statement in the program by using the WITH clause, as shown in the following SQL: SELECT EMPNO, FIRSTNME, LASTNAME FROM DSN8810.EMP WITH UR; The WITH clause allows an isolation level to be specified at the statement level in an application program. However, the restriction that the UR isolation level can be used with read-only SQL statements only still applies. CAUTION If you are running on a pre-V6 DB2 subsystem, be aware that dirty read processing requires type-2 indexes. The UR isolation level is incompatible with type-1 indexes. If the plan or package is rebound to change to UR isolation, DB2 does not consider any access paths that use a type-1 index. If an acceptable type-2 index cannot be found, DB2 chooses a table space scan. When to Use Dirty ReadsWhen is using UR isolation appropriate? The general rule of thumb is to avoid UR whenever the results must be 100% accurate. Examples would be when
In general, most current DB2 applications are not candidates for dirty reads. However, in a few specific situations, the dirty read capability is of major benefit. Consider the following cases in which the UR isolation level could prove to be useful:
|
< Day Day Up > |