【事务隔离级别】数据库事务隔离级别-UNDERSTANDING ISOLATION LEVELS

时间:2023-03-09 20:24:28
【事务隔离级别】数据库事务隔离级别-UNDERSTANDING ISOLATION LEVELS

参考链接:ISOLATION LEVELS

ISOLATION LEVELS

In a database system, concurrent transactions are processed in “isolation” from each other. The level of isolation determines how transactions can affect each other.

UNDERSTANDING ISOLATION LEVELS

READ-UNCOMMITTED

Here transactions can see changes to data made by other transactions that are not yet committed.

In other words, transactions can read data that eventually may not exist, given that other transactions can always rollback the changes without commit. This is known as a dirty read. Effectively, READ-UNCOMMITTED has no real isolation at all.

  • 事务A可以读取到事务B还未 commit的数据
  • 若事务B回滚数据,则事务A将产生脏读

READ-COMMITTED

Here dirty reads are not possible. Uncommitted changes remain invisible to other transactions until the transaction commits.

However, at this isolation level SELECT queries use their own snapshots of committed data, that is data committed before the SELECT query executed. As a result, SELECT queries, when run multiple times within the same transaction, can return different result sets. This is called a non-repeatable read.

  • 事务A不可以读取到事务B还未 commit的数据,即:未commit的数据,对其他事务不可见;
  • 不会产生脏读
  • 同一事务的多次select查询,使用的数据快照可能不同,因此查询结果可能不同,即:不可重复读

REPEATABLE-READ

Here non-repeatable reads are not possible. Snapshots taken for the SELECT query are taken the first time the SELECT query runs during the transaction.

The snapshot remains in use throughout the entire transaction for the SELECT query. It always returns the same result set. This level does not take into account changes to data made by other transactions, regardless of whether or not they have been committed. IN this way, reads remain repeatable.

  • 不会产生脏读不可重复读问题
  • 同一事务的多次select查询,使用的数据快照相同,都使用第一次select时的数据快照,而不管其他是否有没有commit更新;

SERIALIZABLE

Here all records accessed within a transaction are locked. The resource locks in a way that also prevents you from appending records to the table the transaction operates upon.

SERIALIZABLE prevents a phenomenon known as a phantom read. Phantom reads occur when, within a transaction, two identical queries execute, and the rows the second query returns differ from the first.

  • 不会产生幻影读(不确定)