当前位置:网站首页>Four characteristics and isolation levels of database transactions
Four characteristics and isolation levels of database transactions
2022-07-04 10:21:00 【Java full stack preacher】
Better reading experience : Four characteristics and isolation levels of database transactions
1. brief introduction
1.1. What is the business
Business (Transaction) The database is operated by one or more SQL Statement is an integral unit of work , Either of these operations are completed , Either roll back . A transaction often corresponds to a complete business .
1.2. Transaction key process
2. Four characteristics of transaction (ACID)
2.1. Atomicity (atomicity)
The transaction is a whole , Can not be further divided ( Just like atoms in chemistry can't be divided anymore ), All requests DML Statement operations must succeed or fail at the same time .
2.2. Uniformity (consistency)
The data before and after the transaction should be consistent . Such as :A Yes 10 Yuan ,B Yes 20 Yuan .B turn 10 Yuan to A, After the transfer A+B Money or 30 element .
2.3. Isolation, ( isolation )
Different transactions are isolated from each other .
2.4. persistence (durability)
When the business is over , Memory data persistent disk .
3. Concurrency issues
3.1. Two kinds of data loss
The first is when two transactions operate on the same data at the same time , When the first transaction rolls back , Override the update data of the second transaction that has been committed , The second transaction causes data loss .
The second refers to when two transactions operate on the same data at the same time , After the first transaction successfully commits the modification result , The modification result that has been committed by the second transaction is covered , Data loss for the second transaction .
3.2. Dirty reading
Dirty read refers to that one transaction reads the data of another uncommitted transaction .
Such as : Business A Read transaction B Uncommitted data , If the business B Not implemented correctly , Roll back , The transaction A What you read is dirty data .
The key point of the problem : Read **「 Not submitted 」** Transaction data .
3.3. It can't be read repeatedly
Non repeatable reading means that a transaction repeatedly reads the same row of data twice , But the results are different .
Such as : During the execution of a transaction , Another transaction commits and modifies the data being read by the current transaction
The key point of the problem : The same transaction occurs more than once **「 The reading conditions are the same 」 The data of , Other affairs are ok 「 Modify this condition 」** The data of .
3.4. Fantasy reading
Unreal reading is that the results of the same two queries of the same transaction are inconsistent .
Such as : During the execution of a transaction , Another transaction adds or deletes data , As a result, the number of the same query results is different for two times .
The key point of the problem : The same transaction occurs more than once **「 Read data with the same conditions 」, Other affairs are ok 「 Add or delete 」** The data of .
*
solve the problem
*
4. Isolation level
There are two types of transactions : Read and write transactions .
4.1. Read uncommitted (Read UnCommitted)
- Multiple transactions can read uncommitted data at the same time
- When a transaction is read, other transactions can also be written
- When a transaction is written, other transactions can also be read
- But not at the same time
Problem solved : Prevent lost updates
4.2. Read submitted (Read Committed)
- Multiple transactions can read committed data at the same time
- When a transaction is read, other transactions can also be written
- When a transaction is written, other transactions prohibit all operations
- But not at the same time
Problem solved : It can effectively prevent dirty reading .
4.3. Repeatable (Repeatable Read)
- Multiple transactions can read committed insert data at the same time , Cannot read the submitted modification data
- When a transaction is read, other transactions can only be read and written
- When a transaction is written, other transactions prohibit all operations
- But not at the same time
Problem solved : It can effectively prevent non repeatable reading and dirty reading (InnoDB The storage engine also solves the unreal reading problem ).
4.4. Serialization (Serializable)
- Transactions can only be executed one after another , Cannot execute concurrently .
Problem solved : It can effectively prevent dirty reading 、 No repeated reading or phantom reading .
But this level can lead to a lot of timeout and lock contention , It is rarely used in practical applications .
5. Some common operations of transactions
5.1. Set transaction auto commit
-- View the current session
show session variables like 'autocommit';
-- View global sessions
show global variables like 'autocommit';
-- Set current session
set session autocommit = 0|1|ON|OFF;
-- Set up global session
set global autocommit = 0|1|ON|OFF;
Options :0|1|ON|OFF
- close :0 and OFF
- Turn on :1 and ON
5.2. Set isolation level
-- View the current session
select @@tx_isolation;
select @@session.tx_isolation;
-- View global sessions
select @@global.tx_isolation;
show global variables like 'tx_isolation';
-- Set current session
set tx_isolation=' Isolation level ';
-- Set up global session
set global transaction isolation level Isolation level ;
Isolation level
- read uncommitted
- read committed
- repeatable read
- serializable
6. summary
Generally speaking , The higher the isolation level of the transaction , The more you can ensure the integrity and consistency of the database , But relatively speaking , Higher isolation level , The greater the impact on concurrency performance . The official default is **「 Repeatable (Repeatable Read)」** But you can also set the isolation level of the database to read committed (Read Committed), It can prevent dirty reading , It also has good concurrency performance , About non repeatable reading 、 The concurrency problems of unreal reading and the second kind of data loss , It can be controlled by adopting pessimistic lock and optimistic lock in the application . Focus on reading more quality articles Get mind map official account input :dt0010
One key, three links
边栏推荐
- Advanced technology management - how to design and follow up the performance of students at different levels
- Golang Modules
- Differences among opencv versions
- Golang Modules
- Ruby时间格式转换strftime毫秒匹配格式
- MySQL develops small mall management system
- Vs201 solution to failure to open source file HPP (or link library file)
- Golang defer
- 【Day2】 convolutional-neural-networks
- Vanishing numbers
猜你喜欢
Hands on deep learning (36) -- language model and data set
Static comprehensive experiment ---hcip1
Tables in the thesis of latex learning
Hands on deep learning (43) -- machine translation and its data construction
Custom type: structure, enumeration, union
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Normal vector point cloud rotation
Hands on deep learning (42) -- bi-directional recurrent neural network (BI RNN)
Hands on deep learning (40) -- short and long term memory network (LSTM)
BGP advanced experiment
随机推荐
Exercise 9-5 address book sorting (20 points)
2021-08-11 function pointer
Development guidance document of CMDB
Es entry series - 6 document relevance and sorting
Hands on deep learning (43) -- machine translation and its data construction
Hands on deep learning (36) -- language model and data set
Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)
Machine learning -- neural network (IV): BP neural network
Hands on deep learning (45) -- bundle search
Hands on deep learning (38) -- realize RNN from scratch
Exercise 9-3 plane vector addition (15 points)
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
原生div具有编辑能力
Exercise 7-2 finding the maximum value and its subscript (20 points)
【Day1】 deep-learning-basics
leetcode1229. Schedule the meeting
Use C to extract all text in PDF files (support.Net core)
Native div has editing ability
Check 15 developer tools of Alibaba
什么是 DevSecOps?2022 年的定义、流程、框架和最佳实践