Business introduction
concept
- Transaction usually refers to a program execution unit (unit) that accesses and may update various data items in the database. If a business operation containing multiple steps is managed by a transaction, then these operations either succeed or fail at the same time.
Why do you need a transaction?
- The introduction of transactions is usually to solve the consistency of database business operations and prevent confusion in business operations.
Transaction operation
- Start transaction start transaction
roll back roll back
commit commit
Case
To realize the case of transfer from Zhang San to Li Si, execute the following sql in the database table.
CREATE TABLE account (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(10),
balance DOUBLE
);
INSERT INTO account (NAME,balance) VALUES ('zhangsan',1000),('lisi',1000);
Transfer sql code
-- 确保每张三和李四的账户余额都是1000块;
UPDATE account SET balance = 1000;
-- 开启事务
START TRANSACTION;
UPDATE account SET balance=balance-500 WHERE NAME='zhangsan';
UPDATE account SET balance=balance+500 WHERE NAME='lisi';
-- 事务没有问题 提交事务
COMMIT;
-- 事务出现问题,则回滚事务
ROLLBACK;
The transfer is successful The
transfer fails, and the transaction is rolled back to the initial state.
mysql transaction operation
In mysql, DML (addition, deletion, modification) statements are generally automatically submitted by default; ORACLE database is submitted manually
Transaction commit method
Automatic submission
- MySQL automatically commits things, and a DML (addition, deletion, modification) statement will commit a transaction.
Submit manually
- Need to open the transaction first, then commit the transaction
Modify the commit method of the transaction
- Check how the transaction was submitted.
SELECT @@autocommit;
Notes: If the value is 1, it means automatic submission (the default value of mysql), if the value is 0, it means.
2. Modify the default commit method of the transaction
Characteristics of the transaction
Atomicity
- The atomicity of a transaction refers to the smallest indivisible unit of operation that either succeeds or fails at the same time.
Endurance
- When the transaction is committed or rolled back, the database will persist the data.
Isolation
- Multiple transactions are independent of each other.
consistency
- The total amount remains unchanged before and after the transaction operation.
Understanding of consistency and atomicity
- Atomic attention state, either all success or all failure, there is no partial success state.
- The consistency is concerned with the visibility of the data. The data in the intermediate state is invisible to the outside, and only the data in the initial state and the final state is visible to the outside.
Transaction isolation level
What is the isolation level of a transaction?
- Multiple transactions are isolated and independent of each other. 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.
Problems with transaction isolation level
Dirty read
Dirty read generally refers to a transaction that is read from one transaction to another uncommitted transaction.
Non-repeatable
- Non-repeatable read (virtual read): In the same transaction, the data read twice is different. For example: Transaction A reads the same data multiple times, and Transaction B updates and commits the data during the multiple reads of Transaction A. As a result, when Transaction A reads the same data multiple times, the results are inconsistent. (Business B continues to modify operations)
Phantom reading
- A transaction operation (DML) all records in the data table, B transaction adds or deletes a piece of data, the first transaction can not query the results of its own modification. (B transaction has been added and deleted)
Isolation level (causes and solutions)
read uncommitted
- Read uncommitted transactions, resulting in dirty reads, repeatability, and phantom reads.
read committed
- Problem solved: Prevent dirty reads.
Problems: The problems of repeatability and phantom reading were not solved.
repeatable read
- Solved problem: Solved the problem of dirty reading and repeatable reading
. Unsolved problem: The phantom reading is still not solved
serializable
- Problems solved: All problems can be solved.
Attention issues
As the isolation level increases, security will become higher and higher, but efficiency will decrease.
MySQL's default isolation level is repeatable read
oracle's default transaction isolation level is read committed
Database transaction isolation level operation
Query transaction isolation level
select @@tx_isolation;
Set transaction isolation level
-- 级别字符串
-- read uncommitted
-- read committed
-- repeatable read
-- serializable
set global transaction isolation level 级别字符串;
- Notes: Note that the connection can only take effect after the setting is completed.
Case
View the dirty read problems caused by uncommitted transactions:
SET GLOBAL TRANSACTION ISOLATION LEVEL read uncommitted;
Close the original window, restart cmd to execute the following statement
select @@tx_isolation;
The isolation level is modified.
Initial state:
start transfer operation
start transaction;
-- 转账操作
update account set balance = balance - 500 where id = 1;
update account set balance = balance + 500 where id = 2;
Set the transaction isolation level to read committed and reset the account balance.
set global transaction isolation level read committed;
UPDATE account SET balance = 1000;
The main problem of the above case is that after transaction B starts the transaction, that is, the data read in the same transaction is inconsistent. The inconsistency problem is mainly reflected in steps 6, 8, and 11.
set global transaction isolation level repeatable read;
UPDATE account SET balance = 1000;
As can be seen from the above figure, transaction A remains consistent after the transfer operation, which is specifically reflected in steps 5, 7, and 9, while transaction B also maintains stable data before transaction A is submitted, which is specifically reflected in steps 6, 8, and 11. , 12 solves the problem of repeatable reading.
At this point, the review of knowledge about transactions in mysql is here.