jdbc--Things Management

Source: Internet
Author: User
Tags savepoint stmt

Case: Bank transfer problem, the database is as follows

Related APIs

setAutoCommit(boolean autoCommit)  Sets the auto-commit mode for this connection to the given State. Sets whether the transaction is committed automatically if set to False, indicating that the transaction is committed manually

setSavepoint()Creates an unnamed savepoint (savepoint) in the current transaction and returns a new object that represents it Savepoint .

void rollback (); Rollback (When an exception occurs, all code that has executed successfully needs to fall back to the state before the transaction started.) )

The Java.sql interface SavePoint public interface A representation of the savepoint savepoint, which is a Connection.rollback point in the current transaction that can be referenced from the method. When a transaction is rolled back to a savepoint, all changes made after the save point are undone.

1. Transfer public void trans1 () {String Sql_zs = "UPDATE account SET money=money-1000 WHERE name= ' Zhang San ';"; String Sql_ls = "UPDATE account SET money=money+1000 WHERE name= ' John Doe ';"; try {con = jdbcu.getconnection ();//default open hermit transaction Con.setautocommit (TRUE);//default value is True, write not write as/*** first Execute SQL ***/PSTMT = Con.preparestatement (Sql_zs);p stmt.executeupdate ();/*** executes SQL ***/pstmt = Con.preparestatement (Sql_ls) for the second time; Pstmt.executeupdate ();} catch (Exception e) {e.printstacktrace ();} finally {Jdbcu.closeall (con, pstmt, null);}}

 

 

When one party has a problem, this time needs to be managed, except the two are submitted successfully, the others roll back the original state

@Test//2. Transfer, using transaction public void Trans2 () {String Sql_zs = "UPDATE account SET money=money-1000 WHERE name= ' Zhang San ';"; /intentional error, the database will be faulted. String Sql_ls = "UPDATE account SET money=money1+1000 WHERE name= ' John Doe ';"; try {con = jdbcu.getconnection ();//default open hermit Transaction//One, set transaction to manual commit Con.setautocommit (FALSE);/*** Execute SQL ***/pstmt First time = Con.preparestatement (Sql_zs);p stmt.executeupdate ();/*** executes SQL ***/pstmt = Con.preparestatement (Sql_ls) for the second time; Pstmt.executeupdate ();} catch (Exception e) {try {//II, exception occurred, rollback TRANSACTION con.rollback (),} catch (SQLException E1) {}e.printstacktrace ();} finally {try { Three, all operation execution succeeds, commits the transaction con.commit (); Jdbcu.closeall (con, pstmt, null);} catch (SQLException e) {}}}

Rollback to the specified code snippet

3. Transfer, use transaction, rollback to specified code segment//First error, and second error, now want to set a restore point between the first and second @testpublic void trans () {//define a tag savepoint sp = null;// First time transfer string sql_zs1 = "UPDATE account SET money=money-1000 WHERE accountname= ' Zhang San ';"; String SQL_LS1 = "UPDATE account SET money=money+1000 WHERE accountname= ' John Doe ';"; /second transfer string sql_zs2 = "UPDATE account SET money=money-500 WHERE accountname= ' Zhang San ';"; String sql_ls2 = "UPDATE1 account SET money=money+500 WHERE accountname= ' John Doe ';";       try {con = jdbcu.getconnection ();//default open hermit transaction Con.setautocommit (FALSE); Set TRANSACTION manual commit/*** First transfer ***/pstmt = Con.preparestatement (sql_zs1);p stmt.executeupdate ();p stmt = Con.preparestatement ( SQL_LS1);p stmt.executeupdate ();//Roll back to this position? SP = Con.setsavepoint (); /*** Second Transfer ***/pstmt = Con.preparestatement (sql_zs2);p stmt.executeupdate ();p stmt = Con.preparestatement (SQL_LS2); Pstmt.executeupdate ();}  catch (Exception e) {try {//rollback (rollback to specified code snippet) con.rollback (sp),} catch (SQLException E1) {}e.printstacktrace ();} finally {try {//Submit con.commit ();} catch (SqlexcEption e) {}jdbcu.closeall (con, pstmt, null);}} 

  

jdbc--Things 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.