Java Transaction Processing

Source: Internet
Author: User

Java transactions. If you perform multiple operations on the database, each execution or step is a transaction. if the database operation is not executed at a certain step or the transaction fails due to an exception, some transactions will not be executed and the transaction will be rolled back, cancel previous operations .....

Note: To use transaction processing in Java, the database must support transactions first. If you use the MySQL transaction function, you must set the MySQL table type to InnoDB to support transactions. OtherwiseProgramBut cannot take effect in the database.

Use JDBC to process transactions in JavaBean

Public Int Delete ( Int Sid ){
DBC = New Databaseconnection ();
Connection Con = DBC. Get Connection ();
Try {
Con. setautocommit (false ); // Change the default commit Method for JDBC transactions
Dbc.exe cuteupdate (
"Delete from Xiao where id =" + Sid );
Dbc.exe cuteupdate (
"Delete fromxiao_content where id ="
+ Sid );
Dbc.exe cuteupdate (
"Delete fromxiao_affix where bylawid ="
+ Sid );
Con. Commit ();
// Submit a JDBC transaction

Con. setautocommit (True);
// Restore the default commit Method for JDBC transactions
DBC. Close ();
Return1;
}
Catch(Exception exc ){
Con. rollback ();
// Roll back the JDBC transaction
Exc. printstacktrace ();
DBC. Close ();
Return-1;
}
}

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.