当前位置:网站首页>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
边栏推荐
- Architecture introduction
- Hands on deep learning (37) -- cyclic neural network
- 如果不知道這4種緩存模式,敢說懂緩存嗎?
- 2021-08-10 character pointer
- Hands on deep learning (41) -- Deep recurrent neural network (deep RNN)
- Three schemes of ZK double machine room
- System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
- Rhcsa operation
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- Delayed message center design
猜你喜欢
Devop basic command
Matlab tips (25) competitive neural network and SOM neural network
leetcode842. Split the array into Fibonacci sequences
【OpenCV 例程200篇】218. 多行倾斜文字水印
If you don't know these four caching modes, dare you say you understand caching?
Custom type: structure, enumeration, union
Architecture introduction
Application of safety monitoring in zhizhilu Denggan reservoir area
Vs201 solution to failure to open source file HPP (or link library file)
uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
随机推荐
Some summaries of the third anniversary of joining Ping An in China
Golang Modules
Idea SSH channel configuration
[200 opencv routines] 218 Multi line italic text watermark
leetcode1-3
MySQL case
Si vous ne connaissez pas ces quatre modes de mise en cache, vous osez dire que vous connaissez la mise en cache?
MySQL develops small mall management system
Go context basic introduction
【Day1】 deep-learning-basics
uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示
Rhcsa12
System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
A little feeling
uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
Introduction to extensible system architecture
Intelligent gateway helps improve industrial data acquisition and utilization
C language pointer interview question - the second bullet
Work order management system OTRs
入职中国平安三周年的一些总结