mysql database | transaction and isolation level

mysql database | brief description of transaction characteristics

Transaction generally refers to something to be done or done. In computer terms, it refers to a program execution unit (unit) that accesses and may update various data items in the database. Transactions are usually caused by the execution of user programs written in high-level database manipulation languages ​​or programming languages ​​(such as SQL, C++, or Java), and are defined by statements (or function calls) in the form of begin transaction and end transaction. A transaction consists of all operations performed between the beginning of the transaction and the end of the transaction.

The characteristics of the transaction: either all completed or all failed!

1. Four characteristics of transaction

1.1 Consistency

After the transaction is executed, the database status is consistent with other business rules. For the transfer business, regardless of whether the transaction is executed successfully or not, the sum of the balances of the two accounts involved in the transfer should be unchanged, which can be understood as the same as the sum of the amounts in the database before and after the transaction.

1.2 Endurance

Once the transaction is submitted successfully, all data operations in the transaction must be persisted in the database. Even if the database crashes immediately after the transaction is submitted, it must be possible to ensure that the data is restored through a certain mechanism when the database is restarted.

1.3 Atomicity

A transaction is an indivisible atomic unit. All operations in the transaction are either all executed successfully or all executed fail.

1.4 Isolation

Isolation means that in concurrent operations, different transactions should be isolated, so that each concurrent transaction will not interfere with each other.

2. Isolate the transaction

2.1 What will happen if you don't isolate it?

  1. Dirty read: A transaction reads uncommitted data of B transaction
  2. Non-repeatable read: The results of multiple queries within one transaction are different , and the data is modified and submitted by another transaction
  3. Phantom read: (Phantom read is a phenomenon that may occur when multiple transactions are executed) Suppose that the T1 transaction changes all the data in the table from 1 to 2, and at the same time, the T2 transaction inserts a new piece of data. After T1 is executed, it is refreshed and found to be more Those who have not been changed, think that they have hallucinations; this is phantom reading

to sum up:

  1. Both phantom reads and non-repeatable reads read the committed transactions of other transactions
  2. Solve non-repeatable read ----> lock line
  3. Solve phantom reading-------> lock table

2.2 Four isolation levels and problems that can be solved

Insert picture description here

  1. Read Uncommitted (Read Uncommitted): The select statement may read different data without locking, the highest concurrency and the worst consistency
  2. Read Committed (Read Committed): Dirty reads can be avoided

(In the case of high concurrent hardly used as two kinds of isolation level)

  1. Repeatable read (Repeatable read): mysql's default isolation level can avoid dirty reads and non-repeatable reads

  2. Serializable (Serializable isolation highest level): using the lock table of the way to avoid dirty reads unrepeatable reads /// phantom read concurrency worst consistency but is preferably

Summary: As above, the isolation level is from low to high, the higher the execution efficiency, the lower the execution efficiency but the safer

Guess you like

Origin blog.csdn.net/weixin_40597409/article/details/115362215