JDBC事务隔离级别

在JDK源代码java.sql.Connection类中,明确定义了JDBC支持的4个类型的事务隔离级别,在Connection类的开头部分,就定义了如下5个常量,TRANSACTION_NONE常量只是用来指示不支持事务。

 /**
 * 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
 * TRANSACTION_REPEATABLE_READ and further prohibits the
 * situation where one transaction reads all rows that satisfy
 * a WHERE condition, a second transaction inserts a row that
 * satisfies that WHERE 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 一个事务可以读取另外一个事物尚未提交的数据,就是俗称“脏读”(dirty read),在没有提交数据时能够读到已经更新的数据
TRANSACTION_READ_COMMITTED 在一个事务中进行查询时,允许读取提交前的数据,数据提交后,当前查询就可以读取到数据。update数据时候并不锁住表
TRANSACTION_REPEATABLE_READ 在一个事务中进行查询时,不允许读取其他事务update的数据,允许读取到其他事务提交的新增数据
TRANSACTION_SERIALIZABLE 在一个事务中进行查询时,不允许任何对这个查询表的数据修改

在Spring中设置食物隔离级别对应的接口:

/**
 * Attempts to change the transaction isolation level for this
 * Connection object to the one given.
 * The constants defined in the interface Connection
 * are the possible transaction isolation levels.
 * 

* Note: If this method is called during a transaction, the result * is implementation-defined. * * @param level one of the following Connection constants: * Connection.TRANSACTION_READ_UNCOMMITTED, * Connection.TRANSACTION_READ_COMMITTED, * Connection.TRANSACTION_REPEATABLE_READ, or * Connection.TRANSACTION_SERIALIZABLE. * (Note that Connection.TRANSACTION_NONE 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 Connection * constants * @see DatabaseMetaData#supportsTransactionIsolationLevel * @see #getTransactionIsolation */ void setTransactionIsolation(int level) throws SQLException;

你可能感兴趣的:(JDBC事务隔离级别)