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

MySQL trigger usage scenarios and method examples

trigger: Trigger usage scenarios and correspondin...

IE8 Developer Tools Menu Explanation

<br />This article has briefly explained the...

Detailed explanation of HTML basics (Part 1)

1. Understand the WEB Web pages are mainly compos...

Detailed analysis of the MySQL slow log opening method and storage format

In development projects, we can monitor SQL with ...

Detailed explanation of CSS pre-compiled languages ​​and their differences

1. What is As a markup language, CSS has a relati...

Native js to achieve accordion effect

In actual web page development, accordions also a...

Solution to 1067 when Mysql starts in Windows

I just started working a few days ago and install...

A Brief Analysis on the Time Carrying Problem of MySQL

The default time type (datetime and timestamp) in...

How to compare two database table structures in mysql

During the development and debugging process, it ...

JavaScript to implement a simple web calculator

background Since I was assigned to a new project ...

A simple method to implement Linux timed log deletion

Introduction Linux is a system that can automatic...

Use Typescript configuration steps in Vue

Table of contents 1. TypeScript is introduced int...