当前位置:网站首页>MySQL transaction learning notes (I) first encounter

MySQL transaction learning notes (I) first encounter

2022-06-25 05:45:00 There is a fish in Beiming

How to start a transaction

stay 《 Introduction to transaction database 》 We have discussed the basic concepts of transactions in 、 nature , So in MySQL How to treat a series of operations as a transaction in . Like this :

BEGIN;  // Open transaction on behalf of 

UPDATE  Student SET name = ' Zhang Sansan ' WHERE id = '1';

COMMIT; //  For and on behalf of BEGIN and COMMIT Between statements , That is, request MySQL Refresh the results to disk 

If only BEGIN, No, COMMIT, So on behalf of BEGIN The following operations on the data have not been applied to the disk , Represents that the transaction is in a partially committed state , When the transaction isolation level is unreadable , other Session Unable to read this update , adopt :

 SHOW VARIABLES LIKE 'transaction_isolation';

You can see MySQL The default isolation level in transactions :

MySQL The default isolation level for transactions

You can see that the current isolation level of my database is repeatable , In this case, other Session You cannot read the update of this uncommitted transaction , So let's verify that :

 The open transaction has not been committed

Now let's open another Session, That is, open another command line interface , See if you can find the corresponding Session Update .

 Non repeatable reading example

You can see name Or zhangsansan , Now we execute Commit Instructions , Commit transaction , Equivalent to request MySQL Refresh the changes made by the transaction to disk . Then look at the other Session Can you find .

 Commit transaction

 An update was found

Automatic submission

Maybe some students will ask , I usually directly execute a single UPDATE sentence , In other Session You can directly find , Isn't that right UPDATE Statements are not transactions , Then why else did I execute UPDATE, other Session You can find it immediately ? Like this :

 Implicitly submit -1

 Implicitly submit -2

If we don't explicitly use BEGIN or START TRANSACTION( Than BEGIN More comprehensive ) To start a business , Then each DML sentence (INSERT,UPDATE,DELETE ) It's an independent business , Automatic submission , This feature is called automatic transaction commit , stay MySQL Through :

SHOW VARIABLES LIKE 'autocommit';

Check whether the feature is enabled :

 Automatic submission

Implicitly submit

If we don't want to use automatic submission, we can use BGEIN and COMMIT Statement to manually submit , Or will autocommit close , But even if we use BGEIN, There is no 100% guarantee MySQL Will not automatically submit , In the following cases :

  • If BEGIN Heel DDL sentence ( A data definition language that defines or modifies database objects ,Data definition language )
  • To use or modify implicitly mysql( This is MySQL The database that comes with it , Call mysql) Tables in the database
  • Transaction control or statements about locking (BEGIN Then came another BEGIN perhaps START TRANSACTION)
  • Or at the moment Session Open in autocommit The switch of
  • Or use LOCK TABLE、UNLOCK TABLES And other statements about locking will also implicitly commit the transaction to which the previous statement belongs
  • Loading data statements , For example, we use LOAD DATA Statement to bring data into the database in batches , It also implicitly commits the transaction to which the previous statement belongs
  • MySQL Some statements copied , Use START SLAVE,STOP SLAVE 、RESET SLAVE An implicit commit will also be triggered when a statement is issued
  • ANALYZE TABLE [ Table name ]、CACHE INDEX、CHECK TABLE [ Table name ] And other statements will also trigger the transaction to which the preceding statement belongs .

Manually abort the transaction

If something happens , I want to rollback or manually abort the transaction , We can use RollBack Instructions , Like this :

ROLLBACK Command execution

What needs to be emphasized here is ,ROLLBACK The sentence is MySQL It is used to provide the statement when the transaction needs to be rolled back manually , If MySQL When a transaction of cannot continue because of some errors encountered during the execution process , The transaction will be rolled back automatically .

Save it

If we were Begin after , Implemented a lot of UPDATE Sentence? , We just want to roll back to a certain point , And then go on with the execution , Please save some time for comrades to come out and speak , Make a few points on the statement corresponding to the transaction , call ROLLBACK At the time of statement , We can rollback to the point where we want to rollback . Examples are as follows :

 Savepoint rollback Demo

Syntax example :

SAVEPOINT  Save point name   //  Generate savepoints 
RELEASE SAVEPOINT  Save point name  //  Delete savepoint 

Another way to start a transaction

Above we all take BEGIN Statement to start a transaction ,MySQL There is another syntax for starting transactions in :

START TRANSACTION [ Modifier ],[ Modifier ],[ Modifier ];

START TRANSACTION be relative to BEGIN The difference between statements is ,START TRANCATION It can be used as a modifier , As shown below :

  • READ ONLY ( read only mode )
Only data reading is allowed in this mode , It is not allowed to write data of permanent table , You can write data for temporary tables , Temporary tables only exist in the current session .

Example :

 read only mode

  • WRITE ONLY Write only mode
  • WITH CONSISTENT SNAPSHOT Read consistency .

The mode of a transaction cannot be read-only , Write only mode again , We allow both reading and reading by default .

原网站

版权声明
本文为[There is a fish in Beiming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202201802040442.html