在JDK源代碼java.sql.Connection
類中,明確定義了JDBC支持的4個類型的事務(wù)隔離級別,在Connection
類的開頭部分,就定義了如下5個常量,TRANSACTION_NONE
常量只是用來指示不支持事務(wù)。
/**
* A constant indicating that transactions are not supported.
*/
int TRANSACTION_NONE = 0;
/**
* A constant indicating that
* dirty reads, non-repeatable reads and phantom reads can occur.
* This level allows a row changed by one transaction to be read
* by another transaction before any changes in that row have been
* committed (a "dirty read"). If any of the changes are rolled back,
* the second transaction will have retrieved an invalid row.
*/
int TRANSACTION_READ_UNCOMMITTED = 1;
/**
* A constant indicating that
* dirty reads are prevented; non-repeatable reads and phantom
* reads can occur. This level only prohibits a transaction
* from reading a row with uncommitted changes in it.
*/
int TRANSACTION_READ_COMMITTED = 2;
/**
* A constant indicating that
* dirty reads and non-repeatable reads are prevented; phantom
* reads can occur. This level prohibits a transaction from
* reading a row with uncommitted changes in it, and it also
* prohibits the situation where one transaction reads a row,
* a second transaction alters the row, and the first transaction
* rereads the row, getting different values the second time
* (a "non-repeatable read").
*/
int TRANSACTION_REPEATABLE_READ = 4;
/**
* A constant indicating that
* dirty reads, non-repeatable reads and phantom reads are prevented.
* This level includes the prohibitions in
* <code>TRANSACTION_REPEATABLE_READ</code> and further prohibits the
* situation where one transaction reads all rows that satisfy
* a <code>WHERE</code> condition, a second transaction inserts a row that
* satisfies that <code>WHERE</code> condition, and the first transaction
* rereads for the same condition, retrieving the additional
* "phantom" row in the second read.
*/
int TRANSACTION_SERIALIZABLE = 8;
隔離級別 | 說明 |
---|---|
READ_UNCOMMITTED | 一個事務(wù)可以讀取另外一個事物尚未提交的數(shù)據(jù),就是俗稱“臟讀”(dirty read),在沒有提交數(shù)據(jù)時能夠讀到已經(jīng)更新的數(shù)據(jù) |
TRANSACTION_READ_COMMITTED | 在一個事務(wù)中進行查詢時,允許讀取提交前的數(shù)據(jù),數(shù)據(jù)提交后,當(dāng)前查詢就可以讀取到數(shù)據(jù)。update數(shù)據(jù)時候并不鎖住表 |
TRANSACTION_REPEATABLE_READ | 在一個事務(wù)中進行查詢時,不允許讀取其他事務(wù)update的數(shù)據(jù),允許讀取到其他事務(wù)提交的新增數(shù)據(jù) |
TRANSACTION_SERIALIZABLE | 在一個事務(wù)中進行查詢時,不允許任何對這個查詢表的數(shù)據(jù)修改 |
在Spring中設(shè)置食物隔離級別對應(yīng)的接口:
/**
* Attempts to change the transaction isolation level for this
* <code>Connection</code> object to the one given.
* The constants defined in the interface <code>Connection</code>
* are the possible transaction isolation levels.
* <P>
* <B>Note:</B> If this method is called during a transaction, the result
* is implementation-defined.
*
* @param level one of the following <code>Connection</code> constants:
* <code>Connection.TRANSACTION_READ_UNCOMMITTED</code>,
* <code>Connection.TRANSACTION_READ_COMMITTED</code>,
* <code>Connection.TRANSACTION_REPEATABLE_READ</code>, or
* <code>Connection.TRANSACTION_SERIALIZABLE</code>.
* (Note that <code>Connection.TRANSACTION_NONE</code> cannot be used
* because it specifies that transactions are not supported.)
* @exception SQLException if a database access error occurs, this
* method is called on a closed connection
* or the given parameter is not one of the <code>Connection</code>
* constants
* @see DatabaseMetaData#supportsTransactionIsolationLevel
* @see #getTransactionIsolation
*/
void setTransactionIsolation(int level) throws SQLException;