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
1. Overview Zabbix is a very powerful and most ...
Linux installation MySQL notes 1. Before installi...
1. Basic Concepts //Any container can be specifie...
Preface The reason why MySQL's innodb engine ...
Preface As you all know, we have encountered many...
When deleting a table or a piece of data in MySQL...
In a complex table structure, some cells span mul...
The main function of the brower module is to dete...
HTML web page hyperlink tag learning tutorial lin...
1. Methods for implementing components:組件名稱首字母必須大...
This article introduces common problems of Xshell...
MariaDB is installed by default in CentOS, which ...
Technical Background This application uses the vu...
Table of contents 1. Route navigation 2. History ...
This article uses examples to illustrate the usag...