Java Transaction Processing

Source: Internet
Author: User

In database operations, a transaction is an inseparable unit of work consisting of one or more SQL statements updated to the database. The entire transaction can be committed to the database only when all operations in the transaction are completed normally. If one operation is not completed, the entire transaction must be canceled. For example, in a bank transfer transaction, assume that Michael Jacob transfers 1000 yuan from his account to Mr. Li's account. The related SQL statement is as follows: update account set monery = monery-1000 where name = 'hangsan' update account set monery = monery + 1000 where name = 'lisi' the two statements must be processed as a completed transaction. This transaction can be committed only when both are successfully executed. If one sentence fails, the entire transaction must be undone. The connection class provides three methods to control transactions: (1) setautocommit (Boolean autocommit): Set whether to automatically commit transactions; (2) Commit (); Commit transactions; (3) rollback (); undo the transaction. In the jdbc api, the transaction is automatically committed by default. That is to say, each SQL statement updated to the database represents a transaction. After the operation is successful, the system automatically calls commit () to submit the transaction. Otherwise, rollback () is called to cancel the transaction. You can disable automatic transaction commit by calling setautocommit (false. Then, multiple SQL statements for updating the database can be used as a transaction. After all operations are completed, commit () is called for overall commit. If one of the SQL operations fails, the Commit () method is not executed, but sqlexception is generated. In this case, you can capture the Exception Code block and call the rollback () method to cancel the transaction. Try {con = Java. SQL. drivermanager. getconnection (dburl, dbuser, dbpass); con. setautocommit (false); stmt = con. createstatement (); stmt.exe cuteupdate ("update account set monery = monery-1000 where name = 'hangsan '"); stmt.exe cuteupdate ("update account set monery = monery + 1000 where name = 'lisi'"); con. commit ();} catch (exception ex) {ex. printstacktrace (); try {con. rollback ();} catch (exception e) {e. printstacktrace () ;}} finally {try {stmt. close (); con. close ();} catch (exception e) {e. printstacktrace ();}}

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.