JDBC's transaction control

Source: Internet
Author: User
Tags rollback savepoint

JDBC's transaction control

Select @ @tx_isolation See how the system is executed

set@ @autocommit = 1 when automatically committed.

When the system executes set@ @autocommit = 0, it is not autocommit

Start transaction Modify the database table at this time, and execute it correctly with commit.

Commit COMMIT statement. The statement following the commit will be the next transaction: A separate unit of work (one or more SQL statements made)

All failures will be completed only if all the transactions have been successfully executed.

Acid Four Properties

A of atomic

Each transaction is an integral part of the whole, only to be completed in order to succeed otherwise all failed (not in the middle of the implementation of the State);

C Consistency

Ensure that the appropriate transaction is in the correct state.

I. Isolation of

The isolation level is implemented by means of locking (and external isolation is not seen externally during execution and can only be seen when execution is completed).

D Persistence of

When the transaction executes correctly, it is persisted permanently.

set@ @artocommit = 1 when automatically committed

A transaction

Rollback ROLLBACK TRANSACTION (back to the state before modification);

Set the save point savepoint S1;

Rollback to save point: rollback work to savepoint S1;

The transaction is not committed and will only be submitted when the commit is executed, and the modification before the savepoint is submitted for permanent retention

Use Conn.setautocommit (0) in JDBC, and modify system not autocommit.

Conn.commit (); Transaction submission

Conn.rollback (); transaction rollback

SavePoint Sp=con.setsavepoint (); Set Save point

Con.rollback (SP); return savepoint

Con.settransactionisolation (); Design isolation Level

Isolation level of things

Dirty reads: Data that is not submitted by the read thread; (read by another transaction when the modification is not committed)

Not repeatable READ: Read the same record two times in one thing, get different data

Phantom reads: The number of records read in a transaction is different

Isolation level

Dirty Read

Do not read repeatedly

Phantom reading

Read not committed READ UNCOMMITTED

U

Read already submitted Read Committed

Repeatable reads REPEATABLE READ

Serializable Serializable

Instance:

public class Testtransaction {

/**

* @param args

*/

public static void Main (string[] args) {

TODO auto-generated Method Stub

Connection Con=null;

PreparedStatement Pst=null;

SavePoint Sp=null;

try {

Con=jdbcutil.getconnection ();

Con.setautocommit (FALSE);

Con.settransactionisolation (Connection.transaction_repeatable_read);

String str= "Delete from student where id=3";

Pst=con.preparestatement (str);

SYSTEM.OUT.PRINTLN ("First SQL statement ... ");

Pst.executeupdate ();

Sp=con.setsavepoint ();

SYSTEM.OUT.PRINTLN ("Second SQL statement ...");

Str= "INSERT into student (Id,name) VALUES (2, ' ttt ')";

Pst=con.preparestatement (str);

Pst.executeupdate ();

Con.commit ();

SYSTEM.OUT.PRINTLN ("Transaction complete ...");

catch (SQLException e) {

TODO auto-generated Catch block

E.printstacktrace ();

try {

Con.rollback (SP);

Con.commit ();

catch (SQLException E1) {

TODO auto-generated Catch block

E1.printstacktrace ();

}

}finally{

try {

Con.setautocommit (TRUE);

catch (SQLException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

Jdbcutil.release (Null,pst, con);

}

}

}

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.