当前位置:网站首页>The third day of mysql45
The third day of mysql45
2022-06-25 16:24:00 【Hurry to Friday】
mysql Native engine MyISAM The engine does not support transactions .
The four characteristics of affairs (ACID):
- Atomicity ( Atomicity): Transactions are the smallest unit of work , Can not be further divided , In business DML Statements must both succeed or fail .
- Uniformity ( Consistency): The data before and after the transaction must be consistent ( The money transferred out and transferred in must be the same )
- Isolation, ( Isolation): Business A And transaction B With isolation ( When multiple transactions are executed at the same time , There may be dirty reading , It can't be read repeatedly Fantasy reading )
- persistence ( Durability): Once a transaction is committed , It changes the data in the database permanently
Transaction isolation level :
read-uncommited( Uncommitted read ): Modification of transactions , Even if it's not submitted , It's also visible to other things , Transactions can read uncommitted data , be called “ Dirty reading ”, There are few applications in practical applications .
read-commmited ( Submit to read ): A transaction starts from the start to the commit , It's invisible to other things , It's also called It can't be read repeatedly , Because twice the same query , There may be different query results .
repeatable-read( Repeatable ): Solved the problem of dirty reading , It ensures that the result of reading records for multiple times in the same transaction is the same , But it can lead to unreal reading , When reading a row of data , Another transaction inserts data , At this time, the amount of data will change when it is read again
serializeable( Serializable ): Forced transaction serialization , Avoid the problem of unreal reading . Each row of data read will be locked , It will cause timeout and lock contention .

When the transaction level is uncommitted read ( Dirty reading ) When , Because business A Even if not submitted , Business A You can also read the results of the data , therefore v1 =v2= v3=2;
When the transaction level is commit read ( Non repeatability ), Business B Before the end of the transaction A You can't see the data , therefore v1 You can only see the previous values v1=1,v2=v3=2.
When the transaction level is repeatable ,v1=v2=1, Because repeatable reading ensures that the data seen during the execution of the transaction is the same result .v3 It's business B View after submitting , therefore v3=2
When the transaction level is serialization , Business B Changing the value to 2 When , It will be locked ; Until transaction A After submitting , Business B Will continue to carry out , all v1=v2=1,v3=2
In execution sql When , The database will create a view , When accessing data, the data in the view shall prevail .
When the transaction isolation level is uncommitted read , Directly return the latest value on the record , No concept of view ,
When the transaction isolation level is non repeatable , This view is in sql Create when executing ,
When the transaction isolation level is repeatable , This view is created when the transaction starts ,
When the transaction isolation level is serialization , Use locking to prevent parallel access to data .
have access to show variables like ‘transaction_isolation%’ Command to view the current transaction isolation level :
mysql When performing each update operation , A rollback operation will be recorded ; The value of the record can be changed back to the value of the previous state through the rollback operation .
Why try not to apply to long transactions :
Long transactions mean that there are very old transaction views , Since these transactions may access any data in the database at any time , So you need to keep the rollback records
Do preservation , It will take up a lot of storage space .
There are two ways to start a transaction :
- Explicitly start transactions and statements ,beigin or start transaction, The corresponding commit statement is commit, The rollback statement is rollback.
- set autocommit = 0. This command will close the thread's auto commit transaction , If you start a select sentence , This transaction starts ,
And will not voluntarily submit , This transaction will always exist , Until active commit perhaps rollback, Or disconnect .
Can pass set autocommit= 1 Start and commit transactions explicitly . For the background of frequent transactions , Set up set autocommit=0 Sure
You don't have to go there every time begin perhaps start transaction To start a transaction , Reduce the interaction of statements . This case can be used commit work and chain.
commit work and chain What is a ?
Transaction control statements have the following :
START TRANSACTION | BEIGIN [WORK]
COMMIT [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
ROLLBACK [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
SET AUTOCOMMIT = {0 | 1}
SAVEPOINT identify : Allows you to create a new savepoint in a transaction , There can be multiple savepoints in a transaction .
RELEASE SAVEPOINT identify: Release a savepoint , If there is no save point, you can execute , An error is thrown back .
ROOLBACK TO [SAVEPOINT] identify : Roll back to savepoint
CHAIN and RELEASE Used to perform additional operations after the transaction is completed .completion_type The value of the system variable determines the nature of the default completion .
completion_type The default value is 0, Indicates that there is no operation , That is to say NO_CHAIN.
When completion_type The value is 1, That is to say CHAIN When , Indicates that a transaction with the same transaction isolation level is started immediately after a transaction is committed ;
When completion_type The value is 2 When , That is to say release When , Indicates that the connection is disconnected after the transaction is committed .
Actual operation records :
select @@autocommit; You can see that the transaction is automatically committed by default .
I'll cut it off first week surface , Then execute start transaction; Then execute the insert statement , But it doesn't execute commit sentence , There is no record to refresh the table ;
Then go ahead and do it commit; Only then did I see that there were a few on the watch .
Truncate again week surface , Execute first start transaction; Then execute the insert statement , Then perform commit and CHAIN;, Refresh the table to find , Has inserted
Some data , And then execute the insert statement , You can see that the prompt is that several lines of data have been validated , But you can't see the new data when you refresh the table , Again
perform commit sentence , You can find some more data in the table . In the second insert operation, the transaction is not started manually , therefore commit and chain
Indeed, another transaction is automatically started .
alike , First start transaction; Start a transaction , And then execute insert sentence , Then perform commit release ( No commit and release),
But there are more data in the table , But I didn't see the prompt to disconnect , Still say dbeaver Active connection again ?
边栏推荐
- What is backbone network
- Bypass technology to talk about 'cross end'
- Bombard the headquarters. Don't let a UI framework destroy you
- Read the configuration, explain the principle and read the interview questions. I can only help you here...
- Why does golang's modification of slice data affect the data of other slices?
- Stop "outsourcing" Ai models! The latest research finds that some "back doors" that undermine the security of machine learning models cannot be detected
- Navicat premium 15 for MAC (database development tool) Chinese version
- About the use of Aidl, complex data transmission
- Geographic location data storage scheme - redis Geo
- leetcode-8. String to integer (ATOI)
猜你喜欢

Xinlou: un voyage de sept ans de Huawei Sports Health

Don't underestimate the integral mall, its role can be great!

Read the configuration, explain the principle and read the interview questions. I can only help you here...
Classic deadlock scenario of multithreading and its solution (philosopher dining problem)

Multiple decorators decorate a function

The style of the mall can also change a lot. DIY can learn about it!

What can one line of code do?

In the wechat environment, H5 jumps to the specified page of the applet

Advanced SQL statement 1 of Linux MySQL database

Alvaria宣布客户体验行业资深人士Jeff Cotten担任新首席执行官
随机推荐
Why does golang's modification of slice data affect the data of other slices?
Swift responsive programming
Native JS dynamically add elements
Function and implementation of closures
What can one line of code do?
Overall MySQL architecture and statement execution process
After flutter was upgraded from 2.2.3 to 2.5, the compilation of mixed projects became slower
Vscode有什么好用的插件?
Hash table, generic
Cocoapods installation in 2021
Read the configuration, explain the principle and read the interview questions. I can only help you here...
In the wechat environment, H5 jumps to the specified page of the applet
加密潮流:时尚向元宇宙的进阶
Learning notes of rxjs takeuntil operator
Xinlou: Huawei's seven-year building journey of sports health
Alvaria announces Jeff cotten, a veteran of the customer experience industry, as its new CEO
Principle analysis of ThreadLocal source code
Inter thread synchronization semaphore control
Precautions for function default parameters (formal parameter angle)
MySQL installation tutorial