The common notation for JDBC manual transaction commit rollback has been that rollback is written after the commit catch:
Try { conn.setautocommit (false); Ps.executeupdate (); Ps.executeupdate (); conn.commit ();} Catch (Exception e) { }
However, there is no point in this rollback:
Once a commit error occurs, it is not committed and rollback is useless
Once commit, it's true, don't roll back.
Find an article with the same opinion as mine:
Rollback submission for <JDBC Transactions >
I thought there was no need to roll back, and even if I had to roll back, I would have to write the commit after the try...catch of the main business, and rollback when the main business went wrong.
Table: JDBC Transaction 3 notation Rollback comparison
ImportJava.sql.*; Public classTESTJDBC { Public Static FinalString URL = "Jdbc:mysql://127.0.0.1/test"; Public Static FinalString user_name = "root"; Public Static FinalString user_pwd = "root"; Private StaticConnection conn =NULL; Private StaticPreparedStatement PS; Private Static voidConnopen ()throwsSQLException {conn=drivermanager.getconnection (URL, user_name, user_pwd); } Private Static voidConnclose ()throwsSQLException {if(PS! =NULL) {ps.close (); PS=NULL; } if(Conn! =NULL) {conn.close (); Conn=NULL; } } Private Static voidTestrollback ()throwsSQLException {connopen (); Try{ conn.setautocommit ( false); String strSQL= "INSERT into customer (UNAME,PWD) VALUES (?, ' 1 ')"; PS=conn.preparestatement (strSQL); //Insert a piece of dataPs.setstring (1, "Goku"); Ps.executeupdate (); //out of the exception if(true) { Throw NewSQLException (); } //Insert a second piece of dataPs.setstring (1, "Eight commandments"); Ps.executeupdate (); //conn.commit ();}Catch(Exception e) {System.out.println ("Exceptional!"); Try{ conn.rollback (); } Catch(SQLException E1) {e1.printstacktrace (); } } Try{ conn.commit (); } Catch(SQLException e) {e.printstacktrace (); } connclose (); } Private Static voidSelectAll ()throwsSQLException {connopen (); PS= Conn.preparestatement ("SELECT * FROM Customer"); ResultSet RS=Ps.executequery (); while(Rs.next ()) {System.out.print (rs.getstring (2) + ":"); System.out.println (Rs.getstring (1)); } connclose (); } Public Static voidMain (string[] args)throwsSQLException {testrollback (); SelectAll (); }}
The meaning of rollback---the research of JDBC transaction rollback