美文网首页
JDBC事务隔离级别

JDBC事务隔离级别

作者: CloudHuang | 来源:发表于2018-04-26 22:11 被阅读35次

在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
 * <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 一个事务可以读取另外一个事物尚未提交的数据,就是俗称“脏读”(dirty read),在没有提交数据时能够读到已经更新的数据
TRANSACTION_READ_COMMITTED 在一个事务中进行查询时,允许读取提交前的数据,数据提交后,当前查询就可以读取到数据。update数据时候并不锁住表
TRANSACTION_REPEATABLE_READ 在一个事务中进行查询时,不允许读取其他事务update的数据,允许读取到其他事务提交的新增数据
TRANSACTION_SERIALIZABLE 在一个事务中进行查询时,不允许任何对这个查询表的数据修改

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

/**
 * 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;

相关文章

  • JDBC事务隔离级别

    在JDK源代码java.sql.Connection类中,明确定义了JDBC支持的4个类型的事务隔离级别,在Con...

  • 探秘数据库中的事务

    本文包括:1、事务概念2、MySQL管理事务3、JDBC控制事务进程4、事务的特性(ACID)5、事务的隔离级别6...

  • Spring 中的事务隔离级别

    什么是事务隔离级别? 事务隔离级别是对事务 4 大特性中隔离性的具体体现,使用事务隔离级别可以控制并发事务在同时执...

  • 事务理解

    问:事务的隔离性和隔离级别有什么关系? 在jdbc中,事务只针对每一个connect连接,而不是说整个库,我可以针...

  • MySQL_tx_isolation

    事务隔离级别 一、数据库事务隔离级别数据库事务的隔离级别有4个,由低到高依次为Read uncommitted 、...

  • MySQL事务隔离级别和实现原理,看这一篇就够了!!!

    经常提到数据库的事务,那你知道数据库还有事务隔离的说法吗,事务隔离还有隔离级别,那什么是事务隔离,隔离级别又是什么...

  • 面试题

    基础知识 1、事务隔离级别 补充: 1、事务隔离级别为读提交时,写数据只会锁住相应的行 2、事务隔离级别为可重复读...

  • 数据库事务相关

    事务隔离级别(tx_isolation)mysql 有四级事务隔离级别 每个级别都有字符或数字编号 级别symbo...

  • 关于Spring的事务Transactional,锁同步,并发线

    Spring事务传播机制和数据库隔离级别 在标准SQL规范中定义了4个事务隔离级别,不同隔离级别对事务处理不同 。...

  • Mysql事务

    事务隔离级别 事务隔离级别有四种:read-uncomitted,read-commited,repeatable...

网友评论

      本文标题:JDBC事务隔离级别

      本文链接:https://www.haomeiwen.com/subject/chfpqttx.html