Jdbc's transaction control and batch processing .doc

Source: Internet
Author: User
Tags savepoint try catch

JDBC transaction control and Batch Processing

I. Transaction concepts and features

1. Transaction concept: refers to the unit of work for a group of atomic operations (execution of a group of SQL statements)

2. All atomic operations in this unit of work are isolated from other transactions during the execution period, so as to avoid confusion due to traffic updates from data sources. All atomic operations in the transaction are either executed in full, or all failed.

3. Set the transaction commit mode to non-automatic commit.

Conn. setautocommit (false );

4. Transactions and acid properties

A Atomicity is a whole, or both are successful. Either.

C. The status is correct before the consistency transaction is executed. The second correct status is successfully executed. If the execution fails. It will return to the previous status.

I isolation: each transaction occurs in its own space and is isolated from other transactions that occur in the system. And the transaction result can be seen only after it is completed. Implemented through the locking mechanism

D. If the persistent modification is successful, the service will be preserved for a long time.

Ii. Transaction Processing

1. Set @ autocommit = 0; SQL statement

2. Start transaction; set the start point of the transaction

3. execute the transaction. Commit; is the commit transaction. Indicates correct execution. Save permanently.

4. rollback: roll back the transaction and cancel the operation. Return to the original status.

5. Set the savepoint to save the name;

Rollback work to savepoint S1; roll back to the Save point. Tip: no submission. You also need to commit the previous commit.

3. Implement transaction operations in Java.

Conn. Setautocommit (false); Modify the submission method, not automatically submitted

In try catch, the last sentence in try indicates no exception. Execute commit

An exception occurred. Put the rollback transaction in the Catch Block.

Code: Connection con = NULL;

Preparedstatement PST = NULL;

Savepointsp = NULL;

Try {

Con = jdbcutil. getconnection ();

Con. setautocommit (false );

String SQL = "delete fromstudent where id = 1 ";

Pst = con. preparedstatement (STR );

System. Out. println ("the first SQL statement ");

Pst. excuteupdate ();

// Set the save point

Savepointsp = con. setsavepoint (); 44 eeeeeeeee

System. Out. println ("second ");

SQL = "update student setname = 'try' where id = 'asd '"

Pst = con. preparestatement (SQL );

Pst.exe cuteupdate ();

Con. Commit (); // submit the transaction

System. Out. println ("transaction completed");

} Catch (sqlexception e ){

 

Try {

// Con. rollback ();

Con. rollback (SP); return to the Save point

} Catch (sqlexception E1 ){

 

}

} Finally {

Con. setautocommit (true); set the submission method back.

// Release resources

}

Iv. isolation level

Dirty reads, non-repeated reads, and Phantom reads may occur.

Isolation level:

Read uncommitted readuncommitted three cases

Read committed readcommitted cannot be repeated

Repeatableread default level. Phantom reading may occur.

Serializable does not show anything

Con. Settransactionisolation (connection .); Constant

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.