Java Reference
In-Depth Information
cessed via a primary key, and to data accessed via a WHERE clause—and when there is a
WHERE clause, the table is locked such that no new records satisfying the clause can be
added for the duration of the transaction. A serialized transaction will always see the
same data each time it issues a query.
TRANSACTION_REPEATABLE_READ
This requires that all accessed data is locked for the duration of the transaction. However,
other transactions can insert new rows into the table at any time. This mode can lead to
phantom reads , where a transaction that reissues a query with a WHERE clause may get
back different data the second time the query is executed.
TRANSACTION_READ_COMMITTED
This mode locks only rows that are written during a transaction. This leads to nonrepeat-
able reads : data that is read at one point in the transaction may be different from data that
is read at another point in the transaction.
TRANSACTION_READ_UNCOMMITTED
This is the least expensive transaction mode. No locks are involved, so one transaction
may read the written (but uncommitted) data in another transaction. This is known as a
dirty read ; the problem here arises because the first transaction may roll back (meaning
the write never actually happens), and hence the second transaction is operating on incor-
rect data.
Databases operate in a default mode of transaction isolation: MySQL starts with a default of
TRANSACTION_REPEATABLE_READ; Oracle and DB2 start with a default of
TRANSACTION_READ_COMMITTED; and so on. There are lots of database-specific per-
mutations here. DB2 calls their default transaction mode CS (for cursor stability) and has dif-
ferent names for the other three JDBC modes. Oracle doesn't support either
TRANSACTION_READ_UNCOMMITTED or TRANSACTION_REPEATABLE_READ.
When a JDBC statement is executed, it uses the database's default isolation mode. Altern-
ately, the setTransaction() method on the JDBC connection can be called to have the
database supply the necessary transaction isolation level (and if the database doesn't support
the given level, the JDBC driver will either throw an exception or silently upgrade the isola-
tion level to the next strictest level it supports).
Search WWH ::




Custom Search