当前位置:网站首页>Deeply understand the characteristics of database transaction isolation

Deeply understand the characteristics of database transaction isolation

2022-07-07 10:50:00 Taylor lance

Many students don't know much about database transaction isolation , After reading it for a long time, I seem to understand it and I don't seem to understand it , Let me talk about my understanding ;
Here is a key point , Do it , Open it manually sql Command window , Knock the command by hand , A certain , A certain .

First , What is business ;
One sql Is a statement a transaction ?
Only the display is written begin ,rollback,commit; Is it a business ?

One sql Statement is a transaction , Database default begin and commit;
Show written , That sentence is even more business , So when reading books, as long as the textbook mentions affairs , You have to understand A statement is also a transaction .
Connect :https://zhuanlan.zhihu.com/p/67808523

 Insert picture description here
Log in to the database first postgres perhaps mysql/oracle

Through the command :

show default_transaction;

Check the default isolation level of the database .

** Serialization (Serializable,SQLite The default mode ):** The highest level of isolation . Two simultaneous transactions 100% Isolation , Each transaction has its own " The world ", Serial execution .
** Repeatable (Repeatable read,MySQL The default mode ):** If a transaction executes successfully and new data is added ( Transaction submission ), This data is visible to other ongoing transactions . But if the transaction succeeds in modifying a piece of data , The modification result is not visible to the running transaction . therefore , Transactions only break through isolation in terms of new data , Still isolate existing data .
** Read committed (Read committed,Oracle、PostgreSQL、SQL Server The default mode ):** Repeatable + New isolation breakthrough . If the transaction A Read the data D, Then the data D To be transacted B modify ( Or delete ) And submit , Business A Read data again D Changes in data ( Or delete ) Is visible . This is called non repeatable (non-repeatable read).
** Read uncommitted (Read uncommitted):** The lowest level of isolation , Yes, read committed + New isolation breakthrough . If the transaction A Read the data D, Then the data D To be transacted B modify ( But did not submit , Business B Still running ), Business A Read data again D when , Data modification is visible . If the transaction B Roll back , The transaction A Second read data D It's meaningless , Because that's business B Changes made that never happened ( It has been rolled back ). This is called dirty reading (dirty read).

Serialization (Serializable,SQLite The default mode )
And then by order :

show transaction_isolation;

View the current isolation level .

Let's first look at the highest isolation level :serializable

begin;
set transaction isolation level serializable;
....
commit;

The isolation level set above is serializable, Before setting , Be sure to add. begin, It means that your current business is serializable Isolation ,commit after , If the next transaction is not set , Is the default isolation level of your database .

What does this isolation level mean ?
You open another window , You don't need to show the addition begin commit, Just add it in a mess , Delete , Modifying data , While changing, you set it on the other side serializable Query in the window of , Have you found , You set up. serializable The data in this window seems to live in a copy of the moment you just entered , Nothing has changed . This is the highest specification isolation .

To sum up :serializable Under isolation level , you are here serializable All operations in the transaction are isolated in the state of the table before you enter the transaction ( It's like giving you copy Took a snapshot , All your operations are on this copy ), You can't see what others have changed .

Repeatable (Repeatable read,MySQL The default mode )
In this state , You can see the submissions added by others . You can't see what others have modified and submitted .
however ,PostgreSQL Achieve more stringent , It is not only required to be repeatable , Unreal reading is not allowed yet . So it seems to be similar to serializable It's the same .

Read committed (Read committed
You can see what others have submitted ..

Read uncommitted (Read uncommitted
You can see what others haven't submitted .

in fact , stay postgresql in , Read uncommitted (read uncommitted) And read submit (read committed) The implementation of is consistent , So in essence PG Only three isolation levels are achieved .

Reference resources :https://www.xluke.info/article/pg-isolation

原网站

版权声明
本文为[Taylor lance]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130622069452.html