How to solve the Mysql transaction operation failure

How to solve the Mysql transaction operation failure

How to solve the Mysql transaction operation failure

Atomicity of transactions: A transaction is a logical unit of work in a database. All operations contained in a transaction are either performed or not performed.

To achieve transaction atomicity, a single commit or rollback command is not enough, because, for example, the commit command only submits the successfully executed DML statements in a transaction to the database. If you want to achieve the atomicity of transactions, you need to use the commit and rollback commands in conjunction with a business logic in the program. The specific business logic code is as follows:

1. Phenomenon

The transaction is opened in the program for insertion, but there is no commit. The data in the table already exists, and the inserted data cannot be deleted even if it is rolled back.

2. Reasons

The Storage Engine of this table is myisam, not innoDB, and does not support transaction rollback()

3. Solution

Use alter table xxxx engine = innoDB ; to change the table to InnoDB engine, and the rollback is normal.

4. Code

 private void testCrud() {
     Connection conn = null; //Connection object PreparedStatement pstmt = null; //Precompiled SQL statement object try{
        //Load MySQL driver Class.forName("com.mysql.jdbc.Driver");
        //Connection string String url = "jdbc:mysql://localhost:3306/test";
        //Establish database connection conn = DriverManager.getConnection(url,"root","");
        //Set the transaction isolation level // conn.setTransactionIsolation(Connection. TRANSACTION_REPEATABLE_READ);
        //Set autocommit to false and start transaction conn.setAutoCommit(false);
        //Update statement with parameters String sql = "INSERT INTO user_info (username ,password ,age )values(?,?,?)";
        //Prepare statement pstmt = conn.prepareStatement(sql);
        //Bind parameters, execute update statement, and subtract 1000 yuan from Zhang San's account balance pstmt.setString(1, "zhangui");
        pstmt.setString(2, "1111");
        pstmt.setInt(3, 300);
        pstmt.execute();
        
        //Bind parameters, execute update statement, and increase the balance of Li Si's account by 1,000 yuan// pstmt.setString(1, "zzzzzzzzzzzzzzzzz"); //Illegal parameters are bound//pstmt.setString(2, "1111111111");
        //pstmt.setInt(3, 500);
        //pstmt.execute(); //SQL exception will be thrown //Commit the transaction //conn.commit();
        System.out.println("Transaction submitted, transfer successful!");
        //Close statement, connection pstmt.close(); conn.close();
      }catch(Exception e){
        try{
          conn.rollback(); //Rollback transaction System.out.println("Transaction rolled back successfully, no records were updated!");
        }catch(Exception re){
          System.out.println("Rollback transaction failed!");
        }
        e.printStackTrace();
      }finally{
        if(pstmt!=null) try{pstmt.close();}catch(Exception ignore){}
        if(conn!=null) try{conn.close();}catch(Exception ignore){}
      }
    
  }

Thank you for reading, I hope it can help you, thank you for your support of this site!

You may also be interested in:
  • Detailed explanation of Mysql transaction processing
  • Analyzing the four transaction isolation levels in MySQL through examples
  • Solve the problem of shrinking Mysql transaction log and log files being too large to shrink
  • Detailed explanation of MySQL stored procedures, cursors, and transaction examples
  • Analysis of common methods of PHP mysqli transaction operations
  • Detailed explanation of the four transaction isolation levels in MySQL
  • NodeJs uses Mysql module to implement transaction processing example
  • Detailed explanation of MySQL database transaction isolation levels
  • Basic learning and experience sharing of MySQL transactions

<<:  React implements the sample code of Radio component

>>:  Detailed explanation of how to solve the problem that the docker container cannot access the host machine through IP

Recommend

Javascript to achieve the effect of closing advertisements

Here is a case study on how to close ads using Ja...

Implementation of MySQL scheduled database backup (full database backup)

Table of contents 1. MySQL data backup 1.1, mysql...

Detailed explanation of VueRouter routing

Table of contents vue router 1. Understand the co...

MySQL table type storage engine selection

Table of contents 1. View the storage engine of t...

A few experiences in self-cultivation of artists

As the company's influence grows and its prod...

Linux loading vmlinux debugging

Loading kernel symbols using gdb arm-eabi-gdb out...

How to open ports to the outside world in Alibaba Cloud Centos7.X

In a word: if you buy a cloud server from any maj...

Solution to the problem that Docker container cannot be stopped or killed

Docker version 1.13.1 Problem Process A MySQL con...

Description of the default transaction isolation level of mysql and oracle

1. Transaction characteristics (ACID) (1) Atomici...

Research on the Input Button Function of Type File

<br />When uploading on some websites, a [Se...

The presentation and opening method of hyperlink a

<br />Related articles: How to prompt and op...

JavaScript implements circular progress bar effect

This article example shares the specific code of ...