_Things_Four characteristics of things ACID&Isolation level of things

  • Four characteristics of things ACID

1. Atomicity (Atomicity)
Each transaction is a whole and cannot be split. All SQL statements in the transaction either execute successfully or fail.

2. Consistency (Consistency)
The state of the database before the transaction is executed is consistent with the state of the database after the execution. For example: the total amount of 2 individuals before the transfer is 2000, and the total amount of 2 individuals after the transfer is also 2000

3. Isolation (Isolation)
transaction and transaction should not affect each other, and maintain an isolated state during execution.

4. Durability (Durability)
Once the transaction is successfully executed, the modification to the database is durable. Even if it is turned off, it is preserved.

  • Isolation level of things

  • Concept:
    Isolated and independent of multiple transactions. But if multiple transactions operate on the same batch of data, it will cause some problems, and setting different isolation levels can solve these problems.

  • There are problems:

    • 1. Dirty read:
      one transaction reads data that is not committed in another transaction
    • 2. Non-repeatable read (virtual read):
      In the same transaction, the data read twice is different. What is required is that the data is consistent when read multiple times in a transaction, which is a problem caused by transaction update
    • 3. Phantom read:
      All records in a transaction operation (DML) data table, and another transaction adds a piece of data, the first transaction cannot query its own modification. This is a problem caused by insert or delete
  • Isolation level:
    1. read uncommitted: read uncommitted
    * problems: dirty read, non-repeatable read, phantom read
    2. read committed: read committed (Oracle)
    * problems generated: non-repeatable read, phantom read
    3. repeatable read: Repeatable read (MySQL default)
    * Problems: phantom reading
    4. serializable: serialization
    * Can solve all problems

      	* 注意:隔离级别从小到大安全性越来越高,但是效率越来越低
      	* 数据库查询隔离级别:
      		* select @@tx_isolation;
      	* 数据库设置隔离级别:
      		* set global transaction isolation level  级别字符串;
    

Database query isolation level:
* select @@tx_isolation;
* After mysql8.0, use select @@transaction_isolation;
database to set the isolation level:
* set global transaction isolation level level string;

  • Demonstration of isolation level of things

  • 1. Demonstration of dirty reading
    Restore the data: UPDATE account SET balance = 1000;

    1. Open window A to log in to MySQL and set the global isolation level to the lowest
      Insert picture description here
    2. Open the B window, and the AB window will open the transaction

    Insert picture description here

    1. Window A updates the account data of 2 individuals, but has not submitted
      and checked.Insert picture description here
  1. B window query account
    found that another transaction has not submitted data.

Insert picture description here
5. Window A rolls back, Window B queries the data and finds that the money is gone.
Insert picture description here

Dirty reading is very dangerous. For example, Zhang San buys goods from Li Si, Zhang San opens the business, transfers 500 yuan to Li Si's account, and then calls Li Si to say that the money has been transferred. Li Siyi inquired about the money and delivered it to Zhang San. Zhang San rolled back the transaction after receiving the goods, and Li Si's checked the money again.

  • Solve the problem of dirty reads:
    raise the global isolation level.
    Set the global isolation level to read committed in window A. Continue the above operation.
    This time window B queries data here, and found that no uncommitted data of another thing has been read .
    Insert picture description here
    A window commit commits the transaction, and B window views the account.
    Insert picture description here
    Conclusion : The read committed method can avoid the occurrence of dirty reads
  • 2. Demonstration of non-repeatable reading
    1. After recovering the data, open window A and set the database query isolation level to read committed

Insert picture description here
2. Open A, B window things, update data in A window, continue to query data in B window, you will find that the data in the two queries are inconsistent.
Insert picture description here
The output results of the two queries are different. Which one is correct? I don't know which time it will prevail. Many people think that this situation is right, there is no need to be confused, of course the latter shall prevail. We can consider such a situation, for example, the banking program needs to output the query results to the computer screen and send text messages to the customer. As a result, the two queries for different output destinations in a transaction are inconsistent, resulting in inconsistent files and screens. The results were inconsistent, and the bank staff did not know which one to use.

  • Solve the problem of non-repeatable read:
    Raise the global isolation level to: repeatable read
    to restore data.

    1. A window set the isolation level: repeatable read
    2. Exit MySQL in window B and enter MySQL in window B
    3. A window update data
    4. B window query

    Insert picture description here

  • 3. Demonstration of phantom reading

The effect of phantom reading cannot be seen in MySQL.
But we can set the transaction isolation level to the highest to prevent the occurrence of phantom reads and restore data.
1. Open window A, set the transaction isolation level to the highest, window A exits MySQL
Insert picture description here

2. Open windows A and B and start the transaction.
3. Perform transfer operations in window A and query in window B, and you will find that the query in window B cannot be executed, only the cursor has been flashing, which is equivalent to that the table is locked.

Insert picture description here

4. Commit the transaction in window A, and the insert statement in window B will run immediately after the transaction in window A is submitted.

Insert picture description here
Conclusion: Using the serializable isolation level, if a transaction is not executed, the SQL of other transactions cannot be executed, and the phantom reading can be blocked

Guess you like

Origin blog.csdn.net/weixin_44664432/article/details/109363487