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:
|
<<: React implements the sample code of Radio component
Here is a case study on how to close ads using Ja...
Table of contents 1. MySQL data backup 1.1, mysql...
Preface When the system space usage is too large ...
Table of contents vue router 1. Understand the co...
Table of contents 1. View the storage engine of t...
As the company's influence grows and its prod...
Loading kernel symbols using gdb arm-eabi-gdb out...
In a word: if you buy a cloud server from any maj...
Docker version 1.13.1 Problem Process A MySQL con...
1. Transaction characteristics (ACID) (1) Atomici...
<br />When uploading on some websites, a [Se...
<br />Related articles: How to prompt and op...
Preface In MySQL, multi-table join query is a ver...
This article example shares the specific code of ...
1. Download MySQL from the official website: This...