Spring事务的隔离级别有哪些?

Spring事务的隔离级别有哪些?

这是一个在面试过程中经常被问到的问题,要结合数据库来进行回答。
先来看一下spring中的源码:

public enum Isolation {

   /**
    * Use the default isolation level of the underlying datastore.
    * All other levels correspond to the JDBC isolation levels.
    * @see java.sql.Connection
    */
   DEFAULT(TransactionDefinition.ISOLATION_DEFAULT),

   /**
    * 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.
    * @see java.sql.Connection#TRANSACTION_READ_UNCOMMITTED
    */
   READ_UNCOMMITTED(TransactionDefinition.ISOLATION_READ_UNCOMMITTED),

   /**
    * 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.
    * @see java.sql.Connection#TRANSACTION_READ_COMMITTED
    */
   READ_COMMITTED(TransactionDefinition.ISOLATION_READ_COMMITTED),

   /**
    * 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").
    * @see java.sql.Connection#TRANSACTION_REPEATABLE_READ
    */
   REPEATABLE_READ(TransactionDefinition.ISOLATION_REPEATABLE_READ),

   /**
    * A constant indicating that dirty reads, non-repeatable reads and phantom
    * reads are prevented. This level includes the prohibitions in
    * {@code ISOLATION_REPEATABLE_READ} and further prohibits the situation
    * where one transaction reads all rows that satisfy a {@code WHERE}
    * condition, a second transaction inserts a row that satisfies that
    * {@code WHERE} condition, and the first transaction rereads for the
    * same condition, retrieving the additional "phantom" row in the second read.
    * @see java.sql.Connection#TRANSACTION_SERIALIZABLE
    */
   SERIALIZABLE(TransactionDefinition.ISOLATION_SERIALIZABLE);


   private final int value;


   Isolation(int value) {
      this.value = value;
   }

   public int value() {
      return this.value;
   }

}

spring中事务隔离级别就是数据库的隔离级别。

一、事务读取问题

事务里一些有问题的读取:脏读,不可重复读,幻象读。

  • 脏读 (dirty reads)
    事务T1更新了一行记录的内容,但是并没有提交所做的修改。事务T2读取更新后的行,然后T1执行回滚操作,取消了刚才所做的修改。现在T2所读取的行就无效了。
  • 不可重复读取 (nonrepeatable read)
    事务T1读取一行记录,紧接着事务T2修改 了T1刚才读取的那一行记录。然后T1又再次读取这行记录,发现与刚才读取的结果不同。这就称为“不可重复”读,因为T1原来读取的那行记录已经发生了变化。
  • 幻像读取 (phantom read)
    事务T1读取一条指定的WHERE子句所返回的结果集。然后事务T2新插入 一行记录,这行记录恰好可以满足T1所使用的查询条件中的WHERE 子句的条件。然后T1又使用相同的查询再次对表进行检索,但是此时却看到了事务T2刚才插入的新行。这个新行就称为“幻像”,因为对T1来说这一行就像突 然出现的一样。

二、事务的隔离级别

  • default (数据库默认事务)
  • read uncommitted (读未提交),这是事务最低的隔离级别,它充许别外一个事务可以看到这个事务未提交的数据。这种隔离级别会产生脏读,不可重复读和幻像读。
  • read committed (读已提交),保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据。这种事务隔离级别可以避免脏读出现,但是可能会出现不可重复读和幻像读。
  • repeatable read (可重复读)这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻像读。它除了保证一个事务不能读取另一个事务未提交的数据外,还保证了避免不可重复读。(mysql默认的事务隔离级别)
    serializable (串行化)这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。除了防止脏读,不可重复读外,还避免了幻像读。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值