IBM


13.5.1 Transaction isolation levels overview

Transaction isolation levels provide a trade-off between accuracy of reads versus concurrent readers. The levels can best be described by the types of read anomalies they permit and forbid. Consider the read anomalies that can occur with two concurrent transactions, T1 and T2:

- Dirty read

T1 reads data that has been modified by T2, before T2 commits.

- Non-repeatable read

Non-repeatable read is caused by fine-grained locks.

T1 reads a record and drops its lock.

T2 updates.

T1 re-reads different data.

- Phantom read

This is a non-repeatable read involving a range of data and inserts or deletes on the range.

T1 reads a set of records that match some criterion.

T2 inserts a record that matches the criterion.

T1 continues processing the set, which now includes records that were not part of the original matching set.

There are four possible settings for the transaction isolation level:

- Repeatable read (TRANSACTION_REPEATABLE_READ)

This setting permits phantom reads and forbids both dirty and unrepeatable reads.

- Read committed (TRANSACTION_READ_COMMITTED)

This setting permits non-repeatable and phantom reads and forbids dirty reads.

- Read uncommitted (TRANSACTION_READ_UNCOMMITTED)

This setting permits all the read anomalies, including dirty reads, non-repeatable reads, and phantom reads.

- Serializable (TRANSACTION_SERIALIZABLE)

This setting forbids all the read anomalies.

The container applies the isolation level as follows:

- For entity beans with container managed persistence (CMP), the container generates code that ensures the desired level of isolation for each database access.

- For session beans and bean-managed persistence(BMP) entity beans, the container sets the isolation level at the start of each transaction, for each database connection.

The transaction isolation level is tied to a database connection. The connection uses the isolation level specified in the first bean that uses the connection. The container throws an IsolationLevelChangeException whenever the connection is used by another bean method that has a different isolation level.

Not all databases support all JDBC isolation levels. Moreover, JDBC definitions for isolation levels might not match the database definition of isolation levels. As an example, DB2 definitions for isolation levels follow the naming conventions used in Transaction Processing: Concepts and Techniques by Gray. Table 13-4 shows a mapping between EJB and DB2 isolation levels.

Table 13-4

JDBC isolation level DB2 isolation level
TRANSACTION_SERIALIZABLE Repeatable Read
TRANSACTION_REPEATABLE_READ Read Stability
TRANSACTION_READ_COMMITTED Cursor Stability
TRANSACTION_READ_UNCOMMITTED Uncommitted Read

Mapping JDBC isolation levels to DB2 isolation levels

To learn more, refer to the documentation provided by your database product.


Redbooks ibm.com/redbooks

Next