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

Detailed explanation of how to use zabbix to monitor oracle database

1. Overview Zabbix is ​​a very powerful and most ...

Linux installation MySQL5.6.24 usage instructions

Linux installation MySQL notes 1. Before installi...

Detailed explanation of CSS3 Flex elastic layout example code

1. Basic Concepts //Any container can be specifie...

How to get the height of MySQL innodb B+tree

Preface The reason why MySQL's innodb engine ...

Introduction to MySQL method of deleting table data with foreign key constraints

When deleting a table or a piece of data in MySQL...

HTML table tag tutorial (34): row span attribute ROWSPAN

In a complex table structure, some cells span mul...

Specific use of the autoindex module in the Nginx Http module series

The main function of the brower module is to dete...

HTML web page hyperlink tag

HTML web page hyperlink tag learning tutorial lin...

React State state and life cycle implementation method

1. Methods for implementing components:組件名稱首字母必須大...

Detailed explanation of Xshell common problems and related configurations

This article introduces common problems of Xshell...

Detailed tutorial on installing mysql 5.7.26 on centOS7.4

MariaDB is installed by default in CentOS, which ...

Vue-cli framework implements timer application

Technical Background This application uses the vu...

JavaScript history object explained

Table of contents 1. Route navigation 2. History ...