A brief introduction to transactions in MySQL


Preface

When a transaction requirement requires the use of multiple DML operations, the business is processed as a whole. During the processing, if there is a failure or exception. We need to return to the initial state where the business started. If the processing is successful, we persist the data to disk. Such a process is a transaction . Transactions are atomic, consistent, isolated, and durable .

Simply put, a transaction refers to a logical set of operations. Each unit that makes up this set of operations either all succeeds or all fails during execution.

Use keywords: commit , rollback , savepoint


1. Characteristics of transactions (ACID)

  1. Atomicity
    means that a transaction is an indivisible whole, and either all transaction operations occur or none occur .
  2. Consistency transactions
    must transition the database from one consistency state to another . For example, the total amount remains unchanged before and after the transfer.
  3. Isolation: The isolation of transactions
    means that when multiple users access the database concurrently, the transactions opened by the database for each user cannot be interfered by the operational data of other transactions, and multiple concurrent transactions must be isolated from each other.
  4. Durability (Durability)
    Once a transaction is committed, its changes to the data in the database are permanent . Even if the database fails, it should not have any impact on its data.

2. MySQL transactions

By default, every SQL statement executed by MySQL is a separate transaction. If you need to include multiple SQL statements in a transaction, you need to start the transaction and end the transaction .

-- 开启事务:
start transaction-- 结束事务:
commit
rollback

Transaction start situation

  1. Connect to the database and execute a DML statement insert, update or delete
  2. After the previous transaction ended, another DML statement was entered.

The end of the transaction

  1. Execute commit or rollback statement
  2. Execute a DDL statement, such as the create table statement. In this case, the commit statement will be automatically executed.
  3. Execute a DDL statement, such as the create table statement. In this case, the commit statement will be automatically executed.
  4. Disconnect from database
  5. A DML statement is executed and the statement fails. In this case, a rollback statement will be executed for the invalid DML statement.

1. Example: SQL statement to implement transaction support

rollback

START TRANSACTION; 
UPDATE account SET balance=balance-10000 WHERE id=1; 
SELECT * FROM account; UPDATE account SET balance=balance+10000 WHERE id=2; 
ROLLBACK;

submit

START TRANSACTION; 
UPDATE account SET balance=balance-10000 WHERE id=1; 
SELECT * FROM account; 
UPDATE account SET balance=balance+10000 WHERE id=2;
COMMIT;

3. Multiple affairs situation

1. Dirty reading

Transaction A reads the data just updated by transaction B, but transaction B rolls back. This will cause the data read by transaction A to be dirty data, which is dirty reading.

2. Non-repeatable reading

Transaction A reads the same data twice, but transaction B modifies and commits the record between the two times, causing the data read by transaction A to be inconsistent.

The difference from dirty reading is that dirty reading is when transaction A reads the uncommitted dirty data of transaction B. Non-repeatable read means that transaction A reads the data submitted by transaction B.

3.Phantom reading

After transaction A completes the query on the data, transaction B performs the operation of adding new data to it. When transaction A queries the data again, it is found that new records have been added for no reason. This is phantom reading.

4. Comparison between phantom reading and non-repeatable reading

The same thing : they are all for another committed transaction.

Difference : Non-repeatable reads usually target the same record, while phantom reads target multiple records. Non-repeatable reading focuses on update and delete, while phantom reading focuses on insert.

Summary : The database prevents the above situations from happening by setting the isolation level of the transaction .

4. Isolation mechanism

1. Classification of isolation mechanisms

  • Read uncommitted
  • read committed
  • repeatable read
  • Serializable _

1.1.1 Uncommitted read

Without isolation control , "dirty data" can be read. Non-repeatable reads may occur, and phantom reads may occur.

1.1.2 Submission degree

Reading of uncommitted data in a transaction is not allowed. The problem of dirty reads can be avoided , but non-repeatable reads and phantom reads may occur . This isolation level is the default isolation level for most databases ( except MySQL ).

1.1.3 Repeatable reading

In order to avoid the problem of non-repeatable read at commit read level. "Exclusive lock" on the records that meet the conditions in the transaction, so that other transactions cannot modify the data operated by the transaction, which can avoid problem of non-repeatable reading. Because only the operation data is locked, phantom reads will occur when other transactions insert or delete data . This isolation level is MySQL's default isolation level .

1.1.4 Serialization

Lock the table during the transaction so that no other transactions can operate on the table data (add, delete, and modify) before the end of the transaction. This avoids dirty reads, non-repeatable reads, and phantom reads , and is the safest isolation. level . But because this operation is blocked, it will seriously affect performance .

important point:

  • The higher the level of the isolation mechanism , the more secure the data and the lower the performance.
  • Setting the isolation level must be done before the transaction

2.Isolation level related operations in mysql

View the current transaction isolation level : SELECT @@TX_ISOLATION

Change the current transaction isolation level : SET TRANSACTION ISOLATION LEVEL One of the four levels.

3.JDBC controls the isolation level of transactions

Connection interface:

TRANSACTION_READ_UNCOMMITTED: A constant that allows dirty reads, non-repeatable reads and phantom reads to occur

TRANSACTION_READ_COMMITTED: Constant to prevent dirty reads; non-repeatable reads and phantom reads may occur

TRANSACTION_REPEATABLE_READ: Constant to prevent dirty reads and non-repeatable reads; phantom reads may occur

TRANSACTION_SERIALIZABLE: Constant to prevent dirty reads, non-repeatable reads and phantom reads.

Setting method:

Connection.setTransactionIsolation(int level);


Guess you like

Origin blog.csdn.net/qq_45263520/article/details/123975923