The meaning of rollback---the research of JDBC transaction rollback

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.