当前位置:网站首页>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;



边栏推荐
猜你喜欢
![[secretly kill little partner pytorch20 days] - [day4] - [example of time series data modeling process]](/img/f3/e51cb80f838faba8dfd90596d6e57b.jpg)
[secretly kill little partner pytorch20 days] - [day4] - [example of time series data modeling process]

C语言基础小操作

【板栗糖GIS】global mapper—如何把栅格的高程值赋予给点

从零开发 stylelint规则(插件)

Finally someone can make the server so straightforward

Dynamic programming -- gliding wing of the strange thief Kidd
![[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point](/img/bb/ea0e78065ba54ff253995faeeb6901.png)
[chestnut sugar GIS] global mapper - how to assign the elevation value of the grid to the point

Cisco vxlan configuration

剑指 Offer 18. 删除链表的节点

How to automatically renew a token after it expires?
随机推荐
Who is promoting the new inflection point of audio and video industry in 2022?
Video summary of my station B
Idea of capturing mobile terminal variant combination
How to create a CSR (certificate signing request) file?
Solitidy - fallback 回退函数 - 2种触发执行方式
Database SQL language 03 sorting and paging
Tornado frame foundation
Why can transformer break into the CV world and kill CNN?
Simple use of qlistview of QT (including source code + comments)
【板栗糖GIS】global mapper—如何把栅格的高程值赋予给点
[Blue Bridge Road -- bug free code] analysis of AT24C02 storage code
C语言基础小操作
C. Divan and bitwise operations
leetcode763. Divide letter interval
Xiaosha's lunch
/Path/to/ idiom, not a command
Database SQL language 05 SQL exercise
You don't know how to deduce the location where HashSet stores elements?
14x1.5cm vertical label is a little difficult, VFP calls bartender to print
剑指 Offer 18. 删除链表的节点