Database systems - things

1. What is the thing

Transaction (Transaction) is an access operation with the updated data in the system consisting of a series pair of 程序执行逻辑单元. In other words: all operations carried out in a Session, at the same time either succeed or fail at the same time.

4 basic characteristics ACID 2. things

Transaction has four basic characteristics, namely:

  • Atomicity (Atomicity)
  • Consistency (Consistency)
  • Isolation (Isolation)
  • Persistent (Duration)

2.1 Atomicity (Atomicity)

Refers to the atomicity of transactions transaction must be a unit operation sequence atoms. The operations in the transaction included in a process execution, allowing only one of two states appear.

  • All successfully executed
  • All failed

Any one of the following failures will lead to failure of the entire transaction, while other operations have been carried out will be revoked and rolled back, only if all the operations are successful, the entire transaction is considered completed successfully.

2.2. Consistency (Consistency)

Consistency means that the transaction is executing the transaction does not undermine the integrity and consistency of data in the database, and then execute a transaction, the database must impose a consistent state before the execution.

For example: If you transfer money from account A to account B, not because of the money deducted account A and B do not add money to the account.

2.3 Isolation

Transaction isolation means in a concurrent environment, concurrent transactions are isolated from each other, a transaction can not be performed by other transactions interference. In other words, different transaction concurrent operation of the same data, 每个事务都有各自完整的数据空间.

A data operation and use of internal transaction is isolated from other concurrent transactions, each transaction can not be performed concurrently interfere with each other.

In standard SQL specification defines four transaction isolation level, different levels of isolation of the transaction process. Four isolation levels are:

  • Read not mentioned (READ_UNCOMMITTED)
  • Read Committed (READ_COMMITTED)
  • Repeatable Read (REPEATABLE_READ,)
  • Sequential read (SERIALIZABLE)

2.4. Persistence (Duration)

Durability of transactions means that once the transaction is committed, the data in the database must be preserved permanently. Even if the server crashes or the system server downtime fault. As long as the database is restarted, it will be able to restore it to the state after the successful conclusion of the transaction.

3. The four isolation characteristics of things may bring problems

The following things may occur when multiple concurrent issues:

  • Dirty read

    Transaction A modifies data, but did not submit the transaction to update the results of B read A uncommitted transactions, if the transaction fails to submit A, B transaction is read dirty data.

  • Non-repeatable read

    Read the same transaction multiple times the same data, read inconsistent results. For example, a transaction before the transaction A B submit results read, and read the results after the submission may be different.

  • Magic Reading

    In the same transaction, the results of the same query repeatedly returned inconsistent. A new record of a transaction, the transaction B before and after the transaction A submission to execute a query, time and again after the discovery of more than the previous record.

This isolation characteristics of things are closely related, and therefore will be below four isolation characteristics Talking dirty reads, phantom reads and non-repeatable read problem.

3.1 Uncommitted Read (READ_UNCOMMITTED)

Read uncommitted, the isolation level allows dirty reads, which is the lowest level of isolation. In other words, if a transaction is processing certain data, and it has been updated, but the transaction has not been completed, and therefore not to commit the transaction; and this at the same time, allow another transaction can access the data. Ie 事物执行的时候允许访问尚未提交的事物. This may lead to a dirty read.

脏读Examples

The following scenarios may occur when the transaction A and B simultaneously execute a transaction:

time Transaction A (deposit) Transaction B (withdrawals)
T1 Begin transaction ——
T2 —— Begin transaction
T3 —— Balance inquiries (1000 yuan)
T4 —— Remove 1000 yuan (Balance 0 yuan)
T5 Balance inquiries (0 yuan) ——
T6 —— Revocation of the transaction (the balance restored 1000 yuan)
T7 Deposit 500 yuan (balance of 500 yuan) ——
T8 Commit the transaction ——

The balance should be 1,500 yuan son. See point T5 time, transaction A balance inquiry at this time is 0, the data is dirty data, he is the cause of transaction B, it is clear that the transaction is not caused by isolation.

3.2 Read Committed (READ_COMMITTED)

Reading has submission is 不同的事物执行的时候只能获取到已经提交的数据. This situation does not appear above the dirty read.

Read Committed to solve the problem of dirty read, but still can not solve the problem repeatable read.

不可重复读Examples
time Transaction A (deposit) Transaction B (withdrawals)
T1 Begin transaction ——
T2 —— Begin transaction
T3 —— Balance inquiries (1000 yuan)
T4 Balance inquiries (1000 yuan) ——
T5 —— Remove 1000 yuan (Balance 0 yuan)
T6 —— Commit the transaction
T7 Balance inquiries (0 yuan) ——
T8 Commit the transaction ——

In fact, in addition to the transaction A query twice, what else did not do anything, the results from the 1000 money becomes 0, and this is non-repeatable read problem.

3.3 repeatable read (REPEATABLE_READ,)

When repeatable read transaction is to ensure that the process, the same data is read repeatedly, and the value of the start time of the transaction data is consistent. Therefore, the transaction limits the level of non-repeatable reads and dirty reads, but phantom reads the data may appear.

幻读Examples

Magic Reading refers to the same transaction operations, execution in two periods before and after the reading of the same data item, inconsistent results may occur.

A transaction time (Statistics Total deposits) Transaction B (deposit)
T1 Begin transaction ——
T2 —— Begin transaction
T3 Statistics Total deposits (1000 yuan) ——
T4 —— Deposited $ 100
T5 —— Commit the transaction
T6 Submitted to total deposits (1100) ——
T7 Commit the transaction ——

Bank staff see the results not the same as when in a transaction more statistical total deposits. If you want to solve the phantom read, you can only use sequential read.

3.4 Sequential Read (SERIALIZABLE)

Reading order is the strictest transaction isolation level. It requires all queued transactions executed sequentially, that is 事务只能一个接一个地处理,不能并发.

3.5 summary

Transaction isolation level comparison

Transaction isolation level Dirty read Non-repeatable read Magic Reading Concurrency model Update conflict detection
Read not mentioned (READ_UNCOMMITTED) allow allow allow pessimistic X
Read Committed (READ_COMMITTED) Ban allow allow pessimistic X
Repeatable Read (REPEATABLE_READ,) Ban Ban allow pessimistic X
Sequential read (SERIALIZABLE) Ban Ban Ban pessimistic X

4 kinds of transaction isolation level down from the higher level of concurrency worse, security is getting higher and higher.

General Data default level is read to commit or repeatable read.

References

  1. https://www.jianshu.com/p/aa35c8703d61
Published 148 original articles · won praise 136 · Views 250,000 +

Guess you like

Origin blog.csdn.net/DlMmU/article/details/104702345