当前位置:网站首页>MySQL - transaction details

MySQL - transaction details

2022-07-06 21:26:00 Shuangchen legend Technology (Newton it College)

One 、 Business (Transaction) Definition
Business : A smallest indivisible unit of work ; Usually a transaction corresponds to a complete business .

● A complete business needs batch DML(insert、update、delete) The statements are jointly completed .

● stay MySQL Only use Innodb The database or table of the database engine supports transactions .

● Transactions can be used to maintain database integrity , Make sure that there are lots of SQL Statement or all execution , Or none at all .

● Business is only with DML The sentence is about , Or say DML Statements have transactions . This has to do with business logic , Business logic is different ,DML The number of sentences is different

●MySQL Transactions are mainly used to handle large amount of operations , Data with high complexity . for instance , In the people management system , You delete a person , You need to delete the basic information of the personnel , Also delete information related to the person , Such as mailbox , Articles, etc , such , These database operation statements constitute a transaction !

Two 、 The four characteristics of affairs (ACID)

Generally speaking , Transactions must be satisfied 4 Conditions (ACID):Atomicity( Atomicity or indivisibility )、Consistency( Uniformity )、Isolation( Isolation or independence )、Durability( persistence )

● Atomicity (Atomicity): A set of transactions , Or success ; Or withdraw , That is, if an error occurs during the execution of a transaction, it will be rolled back to the state before the start of the transaction .

● Uniformity (Consistency) : A transaction, whether before or after the beginning , The integrity of the database has not been destroyed . Therefore, the written data must fully comply with all preset rules ( Data accuracy 、 Serialization and subsequent databases can spontaneously complete the scheduled work ).

● Isolation, (Isolation): The database allows multiple transactions to read, write and modify its data simultaneously , Isolation can prevent data inconsistency caused by cross execution when multiple transactions are executed concurrently . Transaction isolation can be divided into :Read uncommitted( Read uncommitted )、Read committed( Read the submission )、Repeatable read( Repeatable )、Serializable( Serialization ).

● persistence (Durability): The changes made to the data after the transaction is completed are permanent , Cannot lose

2.1 Isolation, (Isolation) The level of

Business A And transaction B There is isolation between

● Isolation has the following 4 Isolation levels :

1、 Read uncommitted :read uncommitted

● Business A And transaction B

● At this isolation level , All things can see the execution results of other uncommitted transactions

● This isolation level is rarely used in practical applications

● Because its performance is not much better than other levels , Read uncommitted data , It's also called dirty reading (Dirty Read)

2、 Read submitted :read committed

● The default isolation level for most database systems ( But it's not MySQL default )

● Satisfies the simple definition of isolation : One thing is invisible to others before it is submitted , This isolation level also supports so-called non repeatable reads (Nonerepeatable Read), Because other instances of the same transaction may have new commit, So the same select May return different results .

3、 Repeatable :repeatable read

●MySQL Default isolation level

● Ensure that multiple instances of the same transaction read data concurrently , You will see the same data lines

● But this will lead to another thorny problem , Fantasy reading (Phantom Read)

● Unreal reading means that when a user reads a range of data rows , Another thing has inserted a new line in this range , When the user reads the data within this range again , You'll find something new " The phantom " That's ok ,Innodb and Falcon The storage engine is controlled by multi version concurrency (MVCC) The mechanism solved the problem

4、 Serialization :serializable

● Highest isolation level

● By forcing things to sort , Make it impossible for transactions to conflict with each other , Thus the problem of unreal reading is solved

● in short , It is to add a shared lock to each read data row

● At this level , It may lead to a large number of timeout phenomena and lock competition

2.2 The role of isolation

If there is no isolation , Several problems that will occur

● Dirty reading (Dirty Read)

One transaction reads data from another uncommitted transaction

● It can't be read repeatedly (NonRepeatable Read)

For a data in the database , Multiple queries within a transaction range return different data values , This is due to the interval between queries , Another transaction modifies and commits the data .

The difference between unrepeatable and dirty reading is that , A dirty read is when a transaction reads dirty data that is not committed by another transaction , A non-repeatable read reads the data committed by the previous transaction .

In some cases , Unrepeatable reading is not a problem , For example, we repeatedly query a certain data, of course, the results of the final query are mainly . But in other cases, problems can occur , For example, for the same data A and B The sequence of queries may be different ,A and B A fight might break out …….

● Fantasy reading (Phantom Read)

Data inserted by another transaction is read in one transaction , It leads to inconsistencies .

Business A Read data according to certain conditions , Period affairs B Insert new data with the same search criteria , Business A When reading again according to the original conditions , Found a transaction B Newly inserted data

3、 ... and 、 Terms about transactions

● Some terms related to transactions :

● Open transaction :Start Transaction

● End of transaction :End Transaction

● Commit transaction :Commit Transaction

● Roll back the transaction :Rollback Transaction

Four 、 How to start a transaction , What is the identification ? How the transaction ends , What is the identification ?

● Open the sign :

● To perform a DML sentence , Start the transaction .

●eg:insert into、update、delete

● End mark :

● Submit : The end of success , Will all DML Statement operation history is synchronized with the data of the underlying hard disk 

● Roll back : The end of failure , Will all DML The statement operation history is cleared 

4.1 Example : Commit of transaction

● Submit ( Transaction success )

mysql> select * from users;

±-----±-----+

| id | name |

±-----±-----+

| 1 | abc |

±-----±-----+

1 row in set (0.00 sec)

mysql> start transaction; # Start business

Query OK, 0 rows affected (0.00 sec)

mysql> insert into users values(2,‘test’); # perform DML sentence

Query OK, 1 row affected (0.00 sec)

mysql> commit; # Submit

Query OK, 0 rows affected (0.00 sec)

mysql> select * from users; #DML The data inserted by the statement already exists in the table

±-----±-----+

| id | name |

±-----±-----+

| 1 | abc |

| 2 | test |

±-----±-----+

2 rows in set (0.00 sec)

4.2 Example : Rollback of transaction

● Roll back ( Transaction failure )

mysql> select * from users;

±-----±-----+

| id | name |

±-----±-----+

| 1 | abc |

| 2 | test |

±-----±-----+

2 rows in set (0.00 sec)

mysql> start transaction; # Start business

Query OK, 0 rows affected (0.00 sec)

mysql> insert into users values(3,‘xx’); # perform DML sentence

Query OK, 1 row affected (0.00 sec)

mysql> rollback; # Roll back

Query OK, 0 rows affected (0.00 sec)

mysql> select * from users; #DML The data inserted by the statement is not written to the table

±-----±-----+

| id | name |

±-----±-----+

| 1 | abc |

| 2 | test |

±-----±-----+

2 rows in set (0.00 sec)

5、 ... and 、 Transaction control statement

1、 Start a transaction

●begin

or

●start transaction

2、 Set the savepoint , There can be multiple savepoints in a transaction

●savepoint Save point name

3、 Commit transaction , And make all changes made in the database permanent

●commit work

or

●commit

4、 Rollback ends the user's transaction , And undo all pending changes

●rollback work

or

●rollback

5、 Delete a transaction savepoint , If no save point is specified , Executing this statement will report an error

●release savepoint Save point name

6、 Roll the transaction back to the marked point

●rollback to Marker points

7、 Set the isolation level of the transaction

●set transaction isolation level[READ UNCOMMITTED|READ COMMITTED|REPEATABLE READ|SERIALIZABLE]

●InnoDB The isolation levels that the storage engine provides for transactions are READ UNCOMMITTED、READ COMMITTED、REPEATABLE READ and SERIALIZABLE

原网站

版权声明
本文为[Shuangchen legend Technology (Newton it College)]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061257463797.html