Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Atomicity Analysis of MySQL transaction

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the atomicity analysis of MySQL transactions. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the atomicity analysis of MySQL transactions.

1. ACID features of transactions:

1) atomicity

2) consistency

3) isolation

4) persistence

Second, for the atomicity of the transaction (A), we know that either all the operations in the transaction are done or none are done. Many people misunderstand this point. Let's look at an example:

1)

Mysql > select * from T4

+-+ +

| | id | name |

+-+ +

| | 1 | A |

| | 2 | B |

| | 3 | C |

+-+ +

3 rows in set (0.00 sec)

2) start a transaction

Mysql > start transaction

Query OK, 0 rows affected (0.00 sec)

Mysql > update T4 set name='AA' where id=1

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

Mysql > update T4 set name='BBBBB' where id=2

ERROR 1406 (22001): Data too long for column 'name' at row 2

Mysql > commit

Query OK, 0 rows affected (0.03 sec)

Mysql > select * from T4

+-+ +

| | id | name |

+-+ +

| | 1 | AA |

| | 2 | B |

| | 3 | C |

+-+ +

3 rows in set (0.01sec)

Is the result expected to be different? how can the id=1 record be executed successfully? shouldn't it all be rolled back?

Many people around you mistakenly understand that if there is a wrong statement (error) in the transaction, then the whole transaction will be rolled back.

What we say is either full success or total failure. Success and failure depend on whether we end up with commit or rollback. Commit executes all statements and rollback undoes all statements.

In the above example, we are commit operation, even if there is an error statement, all statements will be executed once, and then commit will flush the modified data to disk for persistence. So for the above operation, we should be rollback.

Personal understanding: after opening a transaction, the wrong statements will be filtered out by the parser. When we commit, there is only a correct statement for MySQL (although we know that there is a statement that is wrong), so when we catch an exception, we should display the transaction rollback instead of commit.

Thank you for your reading. the above is the content of "atomicity Analysis of MySQL transactions". After the study of this article, I believe you have a deeper understanding of the atomicity analysis of MySQL transactions, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report