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

A case study of whether MySQL supports transaction nesting

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

Share

Shulou(Shulou.com)06/01 Report--

Does MySQL support case studies of transaction nesting? This problem may be often seen in our daily study or work. I hope you can gain a lot from this question. The following is the reference content that the editor brings to you, let's take a look at it!

Recently, we have encountered using MySQL to open transactions for many times, resulting in data confusion. The pseudo code is as follows:

Begin;# operation 1 beginscape # operate 2rollback

After the execution, the data of operation 1 is actually written, and only the data of operation 2 is rolled back. When the first transaction is not committed or rolled back, and the second transaction is opened, the first transaction is automatically committed.

This is obviously not in line with psychological expectations, and it is not possible to roll back some operations. So the question is, does MySQL support transaction nesting?

It is difficult to answer this question accurately. Yes or no!

First of all, if you call begin multiple times, it is certainly impossible to nest transactions first in MySQL. After a reminder from a friend in the group, I learned that there are sentences called savepoint and rollback to in MySQL.

Sample code:

DROP TABLE IF EXISTS `test`; CREATE TABLE `test` (`id` int (10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar (255) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; begin; insert into `test` (`name`) values ('111'); SAVEPOINT p1; insert into `test` (`name`) values (' 222'); ROLLBACK TO p1; commit

In the final execution result, there is only 111data in the test table, which realizes the rollback operation of part of the operation. In the same way, the problem of opening a transaction multiple times and causing the previous transaction to be committed is avoided.

Perhaps savepoint and rollback to statements cannot be called transaction nesting, nor can it be said that MySQL supports or does not support transaction nesting. In short, some transaction nesting features can be achieved through savepoint and rollback to.

Thank you for reading! After reading the above, do you have a general understanding of the case study on whether MySQL supports transaction nesting? I hope the content of the article will be helpful to all of you. If you want to know more about the relevant articles, you are welcome to follow the industry information channel.

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

Wechat

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

12
Report