当前位置:网站首页>MySQL transaction
MySQL transaction
2022-06-30 05:53:00 【Sxiny Xin】
Catalog
One 、 Concept of transactions
A transaction is a mechanism 、 A sequence of operations , Contains a set of database operation commands , And all the commands as a whole to submit or revoke the operation request to the system , That is, this set of database commands is either executed , Either not .
A transaction is an indivisible unit of work logic , When performing concurrent operations on a database system , Transactions are the smallest unit of control .
Transaction is suitable for the scenario of multi-user operating database system at the same time , Like a bank 、 Insurance companies and securities trading systems, etc .
Transactions ensure the consistency of data through the integrity of transactions .
Two 、 Characteristics of the transaction
ACID, In a reliable database management system (DBMS) in , Business (transaction) It should have four characteristics :
Atomicity (Atomicity)、 Uniformity (Consistency)、 Isolation, (Isolation)、 persistence (Durability).
These are the characteristics of a reliable database .
Atomicity
A transaction is an indivisible unit of work , Either the operations in the transaction occur , Or none at all .
A transaction is a complete operation , The elements of a transaction are indivisible .
All elements in a transaction must be committed or rolled back as a whole .
If any element in the transaction fails , Then the whole transaction will fail .
Uniformity
Before and after the transaction , The database integrity constraint is not broken
When the transaction is complete , The data must be in a consistent state .
Before the transaction starts , The data stored in the database is in a consistent state .
In an ongoing transaction , The data may be in an inconsistent state .
When the transaction completes successfully , The data must return to the known consistent state again .
Isolation,
In a concurrent environment , When different transactions manipulate the same data at the same time , Each transaction has its own full data space .
All concurrent transactions that modify data are isolated from each other , Indicates that the transaction must be independent , It should not in any way depend on or affect other transactions .
A transaction that modifies data can access that data before another transaction that uses the same data starts , Or access the data after another transaction using the same data ends .
There are several kinds of interactions between transactions , Respectively :
Dirty reading : One transaction reads uncommitted data from another transaction , And it's possible to roll back this data .
It can't be read repeatedly : Two identical queries in a transaction return different data . This is caused by the commit of other transaction modifications in the system during query .
Fantasy reading : A transaction modifies the data in a table , This modification involves all data rows in the table . meanwhile , Another transaction also modifies the data in this table , This modification is to insert a new row of data into the table . that , The user operating the previous transaction will find that there are no modified data rows in the table , It's like an illusion .
Lost update : Two transactions read the same record at the same time ,A Change the record first ,B Also modify the record (B I do not know! A A modified ),B After submitting the data B The result of the modification covers A Modification results of .
3、 ... and 、Mysql And the isolation level of things
read_uncommitted : Read uncommitted data : Don't solve dirty reading
**read_committed:** Read submitted data : Can solve dirty reading
**repeatable_read:** Reread read : Can solve dirty reading and It can't be read repeatedly —mysql default
**serializable:** Serialization : Can solve Dirty reading It can't be read repeatedly and Virtual reading — It's like a lock table
mysql The default transaction level is repeatable read , and Oracle and SQL Server yes read committed .
3.1 Query global transaction isolation level
show global variables like '%isolation%';
select @@global.tx_isolation;

3.2 Query session transaction isolation level
show session variables like '%isolation%';
select @@session.tx_isolation;
select @@tx_isolation;

3.3 Set the global transaction isolation level
set global transaction isolation level serializable;

3.4 Set session transaction isolation level
set session transaction isolation level read committed;

Four 、 Transaction control statement
begin or start transaction: Open a transaction explicitly .
commit or commit work: Commit transaction , And make all changes that have been made to the database permanent .
rollback or rollback work: Rolling back ends the user's transaction , And undo all pending changes .
savepoint s1: Use savepoint Allows you to create a rollback point in a transaction , There can be more than one... In a transaction savepoint;“s1” Represents the name of the rollback point .
rollback to [savepoint] S1: Roll back the transaction to the marked point .
4.1 Test commit transactions
Build a bank surface 

4.2 Transaction rollback


4.3 Create rollback point



4.4 Use set Set control transaction
stay MySQL in , When we type the command , The system is automatically helping us begin And commit, No need for us to enter manually . This is because MySQL By default, the database is set to auto submit
# see Mysql Medium AUTOCOMMIT value
show variables like 'autocommit';

Turn off auto submit
# Disable auto submit ( Status as OFF)
SET AUTOCOMMIT=0;



边栏推荐
- Inno setup the simplest user-defined interface effect
- Xiaosha's lunch
- Projet Web de déploiement du serveur Cloud
- Solidity - 安全 - 重入攻击(Reentrancy)
- Detailed explanation of issues related to SSL certificate renewal
- [chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point
- Did you know that WPS can turn on eye protection mode?
- Switch to software testing and report to the training class for 3 months. It's a high paying job. Is it reliable?
- [ansible series] fundamentals -01
- Today, Ali came out with 35K. It's really sandpaper that wiped my ass. it showed me my hand
猜你喜欢
![09- [istio] istio service entry](/img/48/86f8ec916201eefc6ca09c45a60a6a.jpg)
09- [istio] istio service entry

电脑查看WiFi使用密码

【LeetCode】236. Nearest common ancestor of binary tree

Here comes the nearest chance to Ali

Implementation of property management system with ssm+ wechat applet

Idea of capturing mobile terminal variant combination

云服务器部署 Web 项目

ECS deployment web project

Codeforces B. MEX and Array

What indicators should safety service engineers pay attention to in emergency response?
随机推荐
inno setup 最简单的自定义界面效果
Cisco vxlan configuration
How does WPS cancel automatic numbering? Four options
We strongly recommend more than a dozen necessary plug-ins for idea development
What kind of answer has Inspur given in the big AI model landing test?
Implementation of property management system with ssm+ wechat applet
Online assignment of C language program design in the 22nd spring of Western Polytechnic University
I have been working as a software testing engineer for 5 years, but I was replaced by an intern. How can I improve myself?
The average salary of software testing in 2022 has been released. Have you been averaged?
UE4_ Editor development: highlight the UI making method according to the assets dragged by the mouse (1)
剑指 Offer 18. 删除链表的节点
UML tools
ECS deployment web project
leetcode763. Divide letter interval
Use of OpenCL thread algebra library viennacl
Codeforces C. Andrew and Stones
Database SQL language 06 single line function
Huxiaochun came to fengshu electronics to sign a strategic cooperation agreement with Zoomlion
Why can transformer break into the CV world and kill CNN?
MySQL存储系统