Isolation Levels and Concurrency
Isolation Levels and Concurrency
Derby provides four transaction isolation levels. Setting the transaction isolation level for a connection allows a user to specify how severely the user's transaction should be isolated from other transactions. For example, it allows you to specify whether transaction A is allowed to make changes to data that have been viewed by transaction B before transaction B has committed.
A connection determines its own isolation level, so JDBC provides an application with a way to specify a level of transaction isolation. It specifies four levels of transaction isolation. The higher the transaction isolation, the more care is taken to avoid conflicts; avoiding conflicts sometimes means locking out transactions. Lower isolation levels thus allow greater concurrency.
Inserts, updates, and deletes always behave the same no matter what the isolation level is. Only the behavior of select statements varies.
To set isolation levels you can use the JDBC Connection.setIsolationLevel method or the SQL SET ISOLATION statement. The names of the isolation levels are different, depending on whether you use a JDBC method or SQL statement. Table 3 shows the equivalent names for isolation levels whether they are set through the JDBC method or an SQL statement.
Table 3. Mapping of JDBC transaction isolation levels to Derby isolation levels
These levels allow you to avoid particular kinds of transaction anomalies, which are described in Table 4.
Table 4. Transaction Anomalies
The transaction isolation level is a way of specifying whether these transaction anomalies are allowed. The transaction isolation level thus affects the quantity of data locked by a particular transaction. In addition, a DBMS's locking schema might also affect whether these anomalies are allowed. A DBMS can lock either the entire table or only specific rows in order to prevent transaction anomalies.
Table 5 shows which anomalies are possible under the various locking schemas and isolation levels.
Table 5. When Transaction Anomalies Are Possible
The following java.sql.Connection isolation levels are supported:
-
TRANSACTION_SERIALIZABLE
RR, SERIALIZABLE, or REPEATABLE READ from SQL.
TRANSACTION_SERIALIZABLE means that Derby treats the transactions as if they occurred serially (one after the other) instead of concurrently. Derby issues locks to prevent all the transaction anomalies listed in Table 4 from occurring. The type of lock it issues is sometimes called a range lock. For more information about range locks, see Scope of Locks.
-
TRANSACTION_REPEATABLE_READ
RS from SQL.
TRANSACTION_REPEATABLE_READ means that Derby issues locks to prevent only dirty reads and non-repeatable reads, but not phantoms. It does not issue range locks for selects.
-
TRANSACTION_READ_COMMITTED
CS or CURSOR STABILITY from SQL.
TRANSACTION_READ_COMMITTED means that Derby issues locks to prevent only dirty reads, not all the transaction anomalies listed in Table 4.
TRANSACTION_READ_COMMITTED is the default isolation level for transactions.
-
TRANSACTION_READ_UNCOMMITTED
UR, DIRTY READ, or READ UNCOMMITTED from SQL.
For a SELECT INTO, FETCH with a read-only cursor, full select used in an INSERT, full select/subquery in an UPDATE/DELETE, or scalar full select (wherever used), READ UNCOMMITTED allows:
- Any row that is read during the unit of work to be changed by other application processes.
- Any row that was changed by another application process to be read even if the change has not been committed by the application process.
For other operations, the rules that apply to READ COMMITTED also apply to READ UNCOMMITTED.
Previous Page
Next Page
Table of Contents
Index