Detailed example of how to implement transaction commit and rollback in mysql

Detailed example of how to implement transaction commit and rollback in mysql

Recently, we need to perform a scheduled migration of the database data. In order to prevent the SQL statement from reporting an error for some reason during the execution process, which may cause data transfer confusion, we need to control the transaction of our script.

First we create a tran_test table

CREATE TABLE tran_test( 
f1 VARCHAR(10) NOT NULL, 
f2 INT(1) DEFAULT NULL, 
PRIMARY KEY (f1) 
)ENGINE=INNODB CHARSET=utf8

I want to insert two pieces of data into tran_test, but in order to prevent errors during the insertion, I want to control the insert statement within a transaction.

At this time, if you check some people's articles, you will often be given such an answer.

START TRANSACTION;  
INSERT INTO tran_test VALUES('A',1); 
INSERT INTO tran_test VALUES('B',2); 
ROLLBACK;

or

START TRANSACTION;  
INSERT INTO tran_test VALUES('A',1); 
INSERT INTO tran_test VALUES('B',2); 
COMMIT;

These SQL statements look very simple, and they can actually commit or rollback.

But can this really achieve our goal? The answer is no.

For example, in the first paragraph, it will ROLLBACK all your SQL statements in the transaction regardless of whether they are correct or not. Such an absolute rollback makes your SQL meaningless.

Therefore, if we want to truly control transactions, my idea is to perform anomaly detection on the SQL to be executed. If there is no exception in the sql, COMMIT, if an exception is caught, ROLLBACK.

At this time, we need to build a stored procedure to capture the exception. COMMIT is performed when the execution is successful, and ROLLBACK is performed when the SQL execution fails.

There are two ways to achieve the effect I want.

The first one is to capture the exception of the SQL we want to execute. We define a variable t_error. When an exception is captured, let t_error=1. Then make a conditional judgment on t_error. If t_error=1, perform ROLLBACK, otherwise perform COMMIT.

DROP PROCEDURE IF EXISTS t_test; 
DELIMITER // 
CREATE PROCEDURE t_test() 
 BEGIN 
  DECLARE t_error INTEGER; 
  DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error = 1; 
  START TRANSACTION; 
     INSERT INTO tran_test VALUES('A',1); 
   INSERT INTO tran_test VALUES('B',2); 
     IF t_error = 1 THEN 
       ROLLBACK; 
     ELSE 
       COMMIT; 
     END IF; 
END// 
CALL t_test();

The other is a simplification of the first one, that is, if an exception is caught, ROLLBACK is performed directly, and if no exception is caught, COMMIT is performed directly

DROP PROCEDURE IF EXISTS t_test; 
DELIMITER // 
CREATE PROCEDURE t_test() 
BEGIN 
DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK; 
START TRANSACTION; 
INSERT INTO tran_test VALUES('A',1); 
INSERT INTO tran_test VALUES('B',2); 
COMMIT; 
END// 
CALL t_test()

In this way, the two insert statements are truly controlled within one transaction.

You can test the above examples this time. If you have other supplements and questions, you can contact the editor directly. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • A brief analysis of SQL examples for finding uncommitted transactions in MySQL
  • How to find out uncommitted transaction information in MySQL
  • Detailed explanation of Mysql transaction isolation level read commit
  • Exploring the impact of indexes and commit frequency on InnoDB table write speed in MySQL
  • Solution to many spaces when PHP submits textarea data to MySQL
  • MySQL implements transaction commit and rollback examples
  • Python connects to MySQL and submits MySQL transaction example
  • JSP + MySQL Chinese garbled characters problem post submission garbled characters solution
  • Detailed explanation of MySQL and Spring's autocommit

<<:  How to solve the problem that the project in eclipse cannot be added to tomcat

>>:  Ubuntu20's tzselect setting time failure problem, Raspberry Pi server (recommended)

Recommend

JS implements a simple todoList (notepad) effect

The notepad program is implemented using the thre...

JavaScript Canvas implements Tic-Tac-Toe game

This article shares the specific code of JavaScri...

Detailed code examples of seven methods for vertical centering with CSS

When we edit a layout, we usually use horizontal ...

Complete steps for vue dynamic binding icons

0 Differences between icons and images Icons are ...

How to implement a password strength detector in react

Table of contents Preface use Component Writing D...

JavaScript to achieve the effect of clicking on the submenu

This article shares the specific code of JavaScri...

Detailed explanation of Nginx configuration required for front-end

Nginx (engine x) is a lightweight, high-performan...

How to collect Nginx logs using Filebeat

Nginx logs can be used to analyze user address lo...

Various transformation effects of HTML web page switching

<META http-equiv="Page-Enter" CONTENT...

Detailed installation and configuration tutorial of mysql5.7 on CentOS

Install Make sure your user has permission to ins...

Some improvements in MySQL 8.0.24 Release Note

Table of contents 1. Connection Management 2. Imp...