java-Transaction Management

Source: Internet
Author: User

1, the concept of business:
a transaction is a logical set of actions that make up the units of this set of operations, or all succeed, or all of them are unsuccessful.

2. Management Affairs:

2.1. Database Default Transactions
The database supports transactions by default, but the default transaction for a database is a SQL statement that is exclusive to a transaction, a pattern that has little meaning.
2.2. Manual control of transactions
If you want to control a transaction yourself:
Start transaction;
--Open the transaction, all SQL after this statement will be in the same transaction, either complete at the same time or not complete at the same time
......
--The SQL in the transaction does not actually modify the data in the database when it is executed
Commit
--Commit a transaction that takes place across the entire transaction's impact on the database
Rollback
--rollback the transaction, canceling the impact of the transaction on the database

    2.3. Control transactions in JDBC
Connection Conn
PreparedStatement pstat
ResultSet rs
When the JDBC program obtains a connection object to the database, by default the Connection object submits the SQL statement sent on it to the database automatically. To turn off this default submission method and have multiple SQL execute in a single transaction, use the following statement:
Conn.setautocommit (false);
--When auto-commit is turned off, Conn will not commit the transaction, and all SQL statements executed on this connection will be in the same transaction, requiring us to commit or rollback manually
conn.commit ();
--Commit a transaction
Conn.rollback ();
--Rolling back transactions
You can also set the rollback point to roll back some transactions.
SavePoint sp = Conn.setsavepoint ();
Conn.rollback (SP);
-note that, after returning to the rollback point, the code before the rollback point has not been rolled back but has not been submitted, and if it is remembered, it will do a commit operation.

 Public classTrandemo { Public Static voidMain (string[] args) {Connection conn=NULL; PreparedStatement PStat=NULL; SavePoint SP=NULL; Try{            //String url = "Jdbc:mysql://ip:3306/database ";String url = "Jdbc:mysql:///day20"; Class.forName ("Com.mysql.jdbc.Driver"); Conn=drivermanager.getconnection (URL,"Root", "root"); //Open TransactionConn.setautocommit (false); //a-520PStat = conn.preparestatement ("Update account Set" + "money=money-?") where Name=? "); Pstat.setdouble (1, 520); Pstat.setstring (2, "a");            Pstat.executeupdate (); //b+520PStat = conn.preparestatement ("Update account Set" + "money=money+?") where Name=? "); Pstat.setdouble (1, 520); Pstat.setstring (2, "B");            Pstat.executeupdate (); //set the rollback pointsp=Conn.setsavepoint (); //b consumption of 1040PStat = conn.preparestatement ("Update account Set" + "money=money-?") where Name=? "); Pstat.setdouble (1, 1040); Pstat.setstring (2, "B");            Pstat.executeupdate (); //man throws an exception            intx = 3/0; //a+1040PStat = conn.preparestatement ("Update account Set" + "money=money+?") where Name=? "); Pstat.setdouble (1, 1040); Pstat.setstring (2, "a");            Pstat.executeupdate (); //Commit a transactionConn.commit (); }Catch(Exception e) {if(conn!=NULL){                if(sp!=NULL){                    Try {                        //Roll back to the SPConn.rollback (SP); //submit the action of the transferConn.commit (); } Catch(SQLException E1) {e1.printstacktrace (); }                }Else{                    Try {                        //rolling back a transaction to open a placeConn.rollback (); } Catch(SQLException E1) {e1.printstacktrace (); }                }            }        }finally{            //Close Database connection ....         }    }}

java-Transaction Management

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.