MySQL database - transactions - introduction, transaction operations, four major features, concurrent transaction issues, transaction isolation level

Table of contents

Introduction

Transaction operations

View/set transaction submission method 

Open transaction 

commit transaction 

Rollback transaction

Four major characteristics ACID

Atomicity

Consistency

Isolation

Durability

Concurrent transaction issues

dirty read

non-repeatable read

phantom reading

transaction isolation level

summary


The content of multi-table query has come to an end. The next thing to learn is a new content - transactions, which will be divided into several points:

  • Introduction
  • Transaction operations
  • The four major characteristics of transactions ACID
  • Concurrent transaction issues
  • isolation level
  • summary

Let’s first have a simple and general understanding of the transaction:

Introduction

A transaction is a set of operations, which is an indivisible unit of work. A transaction will submit or revoke operation requests to the system as a whole, that is, these operations will either succeed at the same time or fail at the same time .

 For example, the following three operations (Zhang San transfers 1,000 yuan to Li Si):

1. Check Zhang San’s account balance;

2. Zhang San’s account balance -1000;

3. John Doe’s account balance is +1000.

under normal circumstances,

We performed operations 1 and 2. If an exception was thrown during the process from 2 to 3, then the balance of Li Si has not completed the operation of +1000; but Zhang San's account balance is still reduced by 1000, which makes Zhang San I lost 1,000 yuan for nothing, which is obviously unreasonable.

Therefore, we need to use transactions to operate and combine these operations.

After starting the transaction, perform operations 1 and 2. When an exception is thrown during the process from 2 to 3, the transaction will be rolled back and return to operation 1;

The transaction is not committed until all operations are completed.

By default, MySQL transactions are automatically committed, which means that when a DML statement is executed, MySQL will immediately and implicitly commit the transaction.

Transaction operations

View/set transaction submission method 

SELECT @@autocommit;   -- 查看事务提交方式

SET @@autocommit = 0;   -- 设置事务提交方式
-- 0代表手动提交事务,1代表自动提交事务

When we change the transaction submission method to manual submission, COMMIT is required every time a SQL statement is executed to submit the transaction. Otherwise, the data is only temporarily modified, but the data is not submitted to the database.

Open transaction 

START TRANSACTION 或 BEGIN;

Only after things are turned on will the executed statements be submitted manually; different from the global settings above.

commit transaction 

COMMIT;

When all operations are correct, we can submit the transaction and submit the data to the database. 

Rollback transaction

ROLLBACK;

When an operation is abnormal, the operation needs to be undone and restarted, which is called a rollback transaction.

Four major characteristics ACID

The four major characteristics here relate to interview questions

Atomicity _ _

  • A transaction is an indivisible smallest unit of operation that either succeeds in its entirety or fails in its entirety.

When a transaction is executed, it will execute a set of SQL statements in the same batch. If an error occurs in one SQL statement, all SQL statements in the batch will be canceled.

Consistency _ _

  • When the transaction is completed, all data must be in a consistent state.

For example, a bank transfer can be broken down into two actions: deducting an amount of funds X from account A, and then adding an amount of funds X to account B. If an error occurs during this process, the entire operation should be rolled back to the initial state. Ensure that the combined account balances of A and B are consistent with the initial state.

Isolation _ _

  • The isolation mechanism provided by the database system ensures that transactions run in an independent environment that is not affected by external concurrent operations.

For example, one transaction is modifying a certain data item, and at this time another transaction also wants to modify the data item. However, due to the existence of isolation, the second transaction will wait for the first transaction to complete the modification before making modifications.

Durability _ _

  • Once a transaction is committed or rolled back, its changes to the data in the database are permanent.

Once a transaction is committed, its changes to the database should be permanent (the data submission is saved on the hard disk), and subsequent other operations or failures should not have any impact on the modifications of this transaction.

Concurrent transaction issues

dirty read

One transaction reads data that another transaction has not yet committed.

non-repeatable read

A transaction reads the same record successively, but the data read twice is different, which is called non-repeatable read.

phantom reading

When a transaction queries data according to conditions, there is no corresponding data row, but when inserting data, it is found that this row of data already exists, as if a "phantom" has appeared.

To solve transaction concurrency problems, you need to set different isolation levels according to different concurrency problems.

transaction isolation level

isolation level dirty read non-repeatable read phantom reading
Read uncommitted (Oracle default) Will appear Will appear Will appear
Read committed won't appear Will appear Will appear
Repeatable Read (MySQL default) won't appear won't appear Will appear
Serializable won't appear won't appear won't appear

View transaction isolation level

SELECT @@TRANSACTION_ISOLATION;

Set transaction isolation level

SET [SESSION GLOBAL /* 设置当前窗口的事务隔离级别 */ ] 
TRANSATION ISOLATION LEVEL [READ UNCOMMITTED | ......];

Note: The higher the transaction isolation level, the more secure the data, but the lower the performance.

Therefore, it is necessary to weigh the balance between transaction concurrency issues and performance and choose an appropriate isolation level.

summary

1. Introduction to the transaction

A transaction is a set of operations that either all execute successfully or all fail.

2. Transaction operations 

START TRANSACTION; -- 开启事务
COMMIT / ROLLBACK; -- 提交/回滚事务

3. Four characteristics of affairs

Atomicity, Consistency, Isolation, Durability

4. Concurrent transaction issues

Dirty read, non-repeatable read, phantom read 

5. Transaction isolation level 

READ UNCOMMITTED、READ COMMITTED、REPEATABLE READ、SERIALIZABLE

END 


Learn from: Dark Horse Programmer - MySQL Data Course

Guess you like

Origin blog.csdn.net/li13437542099/article/details/132590341