Database MySQL - Transaction

Table of contents

1. Business Introduction

 Two, transaction operation

3. Four characteristics of transactions (ACID)

4. Concurrent transaction issues

1. Dirty read

 2. Non-repeatable read

 3. Phantom reading

 5. Transaction isolation level


1. Business Introduction

A transaction is a collection of a group of operations, which is an indivisible unit of work. A transaction will submit or withdraw an operation request to the system together with all operations as a whole, that is, these operations either succeed at the same time or fail at the same time .

Example: The most typical example: bank transfer (Zhang San transfers 1,000 yuan to Li Si)

  1. normal circumstances:
    Query Zhang San's account balance
    Zhang San's account balance - 1000
    Lisi account balance +1000
  2. Abnormal situation: (At this time, Zhang San’s reduced money cannot be added to Li Si)

    Therefore, if we want to solve this kind of exception, we need to solve it through the transaction of the database, define the above three operations in a transaction, or all of them are executed successfully, or all of them fail.

  3. Solution: (transaction)

Rolling back a transaction refers to restoring the previously temporarily modified data, that is, initialization.

 For the MySQL database, the transaction is automatically submitted by default, that is to say, when we add, delete or modify a certain statement, the transaction will be submitted to the database after the statement is completed, and the data in the database will be changed immediately.

 Two, transaction operation

  • View/set transaction submission method:

         SELECT @@autocommit; (autocommit)

          SET @@autoconnit = 0; (commit manually)

  • Commit transaction: START TRANSACTION or BEGIN;
  • Commit the transaction: COMMIT;
  • Rollback transaction: ROLLBACK;

The above example operation:

-- 创建表添加数据
create table account(
    id int auto_increment primary key comment '主键',
    name varchar(10) comment '姓名',
    money int comment '金额'
)comment '账户表';
insert into account (id, name, money) values (null,'张三',2000),(null,'李四',2000);

update account set money = 2000 where name = '张三' or  name = '李四';

-- 方式一:
select @@autocommit;

set @@autocommit = 0;

-- 转账操作
-- 1.查询张三账户余额
select * from account where name = '张三';
-- 2.张三账户余额 -1000
update account set money = money - 1000 where  name = '张三';
程序抛出异常...
-- 3.李四账户余额 +1000
update account set money = money + 1000 where  name = '李四';

-- 提交事务
commit ;
-- 回滚事务
rollback ;



-- 方式二:
-- 转账操作

-- 开启事务
start transaction ;

-- 1.查询张三账户余额
select * from account where name = '张三';
-- 2.张三账户余额 -1000
update account set money = money - 1000 where  name = '张三';
程序抛出异常...
-- 3.李四账户余额 +1000
update account set money = money + 1000 where  name = '李四';

-- 提交事务
commit ;
-- 回滚事务
rollback ;

3. Four characteristics of transactions (ACID)

  • Atomicity : A transaction is an indivisible minimum unit of operation, either all succeed or all fail .
  • Consistency ( Consistency ): When the transaction is completed, all data must be kept in a consistent state.
  • Isolation ( lsolation ): The isolation mechanism provided by the database system ensures that transactions run in an independent environment that is not affected by external concurrent operations.
  • Durability ( Durability ): Once a transaction is committed or rolled back, its changes to the data in the database are permanent.
     

4. Concurrent transaction issues

Concurrent transaction problems refer to some problems caused by transaction A and transaction B operating a certain database or even a certain table at the same time.

question describe
dirty read A transaction reads data that has not been committed by another transaction.
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.

1. Dirty read

After the first step of transaction A is executed, the second step is executed, but the transaction is not committed until the second step of transaction A is completed. After the second step of transaction A is completed, the data in the database table will be updated. At this time, another step of transaction B is performed. Query operation, and what transaction B queried is exactly the data updated by the second step operation of transaction A, but transaction A did not commit.

 2. Non-repeatable read

After transaction A executes the first step, it starts to execute the second step and at the same time transaction B executes the update operation. After the operation is completed, the data is submitted to the database, and then transaction A performs the third step. At this time, the first step and the third step The operations are the same query statement, but the results are different.

 3. Phantom reading

Transaction A executes the first step, and transaction B also executes the operation at this time. After the execution, the data is submitted to the database, and then transaction A executes the second step, but finds that the execution fails, and the corresponding primary key already exists, but when the third step is executed , the execution result is empty.

 5. Transaction isolation level

Based on our understanding of concurrent transaction issues above, we will learn the transaction isolation level to solve these problems:

isolation level dirty read non-repeatable read Phantom reading
Read uncommitted
Read committed
Repeatable Read (default)
Serializable
  • View transaction isolation level: SELECT @@TRANSACTION_ISOLATION;
  • Set transaction isolation level: SET [ SESSION | GLOBAL ] TRANSACTION ISOLATON LEVEL { READUNCOMMITTED | READ COMMITED│REFPEATABLE READ│ SERALZABLE }

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

Guess you like

Origin blog.csdn.net/hdakj22/article/details/129764609