Isolation Level
Isolation Level
Isolation levels control how transaction data is isolated from other transactions, affecting the visibility and impact of changes made during a transaction. Proper isolation prevents issues such as dirty, non-repeatable, and repeatable reads, which can disrupt data integrity and application logic.
Why Use Isolation Levels?
Isolation levels in DB2 help in managing the delicate balance between allowing concurrent access to the database (which increases performance) and protecting data integrity (which ensures that the data is correct and stable). The choice of isolation level affects:
- Concurrency: Higher concurrency allows more transactions to run simultaneously but can lead to more conflicts.
- Data Integrity: Higher data integrity ensures data is accurate and stable but can reduce concurrency.
Different Isolation Levels in DB2 -
DB2 supports several isolation levels, each designed for specific scenarios:
Uncommitted Read (UR) -
- Description: Transactions can read data changes made by other transactions even before they are committed.
- Use Case: Suitable for situations where absolute accuracy is not required, like generating reports from large volumes of data where performance and speed are prioritized over precision.
- Example: A report-generating transaction might use UR to fetch data quickly without waiting for other transactions to commit.
Cursor Stability (CS) -
- Description: The current row under a cursor is locked, preventing other transactions from changing it, but changes to other rows are visible.
- Use Case: Useful in applications where the user interacts with one row at a time, like record browsing in a database.
- Example: When a user is editing a specific record in a database form, CS ensures no other users can change that row, but changes to other rows by other users are allowed.
Read Stability (RS) -
- Description: Once a row is read, it is locked for the duration of the transaction, preventing other transactions from modifying it.
- Use Case: Good for scenarios where transactions need to ensure that records won't change throughout their execution but can tolerate not seeing new records added by others.
- Example: In banking systems, when calculating interest for an account, RS can ensure that the account records don't change during the calculation.
Repeatable Read (RR) -
- Description: All rows referenced during the transaction are locked, preventing other transactions from updating or inserting rows that would affect the current transaction.
- Use Case: Ideal for scenarios where it is crucial that selected rows do not change throughout the transaction and no new rows that meet transaction-specific search criteria are added.
- Example: In an inventory system, RR can be used to ensure that the quantity of items does not change during the transaction processing period.
Summary of isolation levels -
Isolation level | Access to uncommitted data | Nonrepeatable reads | Phantom reads |
---|---|---|---|
Repeatable read (RR) | Not possible | Not possible | Not possible |
Read stability (RS) | Not possible | Not possible | Possible |
Cursor stability (CS) | Not possible | Possible | Possible |
Uncommitted read (UR) | Possible | Possible | Possible |
Guidelines for choosing an isolation level -
Application type | High data stability required | High data stability not required |
---|---|---|
Read/write transactions | RS | CS |
Read-only transactions | RR or RS | UR |