当前位置:网站首页>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
边栏推荐
- Laravel文档阅读笔记-How to use @auth and @guest directives in Laravel
- Legion is a network penetration tool
- libmysqlclient. so. 20: cannot open shared object file: No such file or directory
- PHP code audit 3 - system reload vulnerability
- Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 2
- Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)
- leetcode842. Split the array into Fibonacci sequences
- Static comprehensive experiment ---hcip1
- Histogram equalization
- Latex arranges single column table pictures in double column format articles
猜你喜欢
Static comprehensive experiment ---hcip1
SQL replying to comments
5g/4g wireless networking scheme for brand chain stores
Histogram equalization
MongoDB数据日期显示相差8小时 原因和解决方案
Latex error: missing delimiter (. Inserted) {\xi \left( {p,{p_q}} \right)} \right|}}
如果不知道這4種緩存模式,敢說懂緩存嗎?
BGP advanced experiment
Hands on deep learning (43) -- machine translation and its data construction
Hands on deep learning (44) -- seq2seq principle and Implementation
随机推荐
PHP code audit 3 - system reload vulnerability
Hands on deep learning (45) -- bundle search
Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 1
【Day2】 convolutional-neural-networks
leetcode1-3
Laravel文档阅读笔记-How to use @auth and @guest directives in Laravel
PHP代码审计3—系统重装漏洞
Map container
MPLS: multi protocol label switching
Qtreeview+ custom model implementation example
【Day1】 deep-learning-basics
今日睡眠质量记录78分
system design
AUTOSAR from getting started to mastering 100 lectures (106) - SOA in domain controllers
Basic principle of servlet and application of common API methods
Rhcsa day 9
Advanced technology management - how to design and follow up the performance of students at different levels
【Day2】 convolutional-neural-networks
Rhcsa learning practice
Hands on deep learning (41) -- Deep recurrent neural network (deep RNN)