JDBC Basic Learning (iv)-Database transactions

Source: Internet
Author: User

I. Basic knowledge of affairs 1. Overview of transactions

In order to ensure the consistency of data in the database, the operation of data should be a discrete group of logical units. When it is complete, the consistency of the data can be maintained, and when part of the operation fails, the entire transaction should be treated as an error, and all operations from the starting point should be rolled back to the start state.

Transaction operations: First define the start of a transaction, and then modify the data, if committed, the data will be persisted, if the fallback (rollback), the database management system will discard all changes to start the state of the transaction.

2. Properties of a transaction

(1) atomicity (atmicity)

Atomicity means that a transaction is an inseparable unit of work, and the operation of the transaction either occurs or does not occur.

(2) Consistency (consistency)

A transaction must transform the database from one consistent state to another consistent state.

(3) Isolation (isolation)

The isolation of a transaction is that the execution of a transaction cannot be disturbed by other transactions, that is, the internal operation of one transaction and the data used are isolated from other concurrent transactions, and cannot interfere with each other in concurrently executing transactions.

(4) Persistence (durability)

Persistence is the fact that once a transaction is committed, it changes the data in the database to be permanent, and the subsequent operations and database failures should not have any effect on it.

Second, the operation of the transaction in JDBC

(1) If there are multiple operations, each operation uses its own individual connection, the transaction cannot be guaranteed.

(2) Steps to use:

A: Before the transaction begins, the transaction is started, that is, the autocommit is canceled. Con.setautocommit (FALSE);

B: If the operation of the transaction succeeds, the transaction is committed. Con.commit ();

C: If an exception occurs, the transaction is rolled back in the catch block. Con.rollback ();

/* Transaction * 1. If there are multiple operations, each operation uses its own separate connection, the transaction cannot be guaranteed. * 2. Step * (1) Start the transaction before the transaction begins, i.e. cancel autocommit. *    Con.setautocommit (FALSE); * (2) If the operation of the transaction succeeds then commits the transaction *    Con.commit (); * (3) ROLLBACK TRANSACTION: If an exception occurs, the transaction is rolled back in the catch block. *    con.rollback (); */@Testpublic void Testtx () {Connection con = null;try{//Ensure that the connection is unique, passing in the same Connnectioncon = Jdbctools.getconnection ();//Start transaction: Cancel Default commit con.setautocommit (FALSE); String sql = "Update person Set salary = salary-500 where name =?"; Jdbctools.update (Con,sql, "Tom"); int i = 1/0; String sql2 = "Update person Set salary = salary + where name =?"; Jdbctools.update (CON,SQL2, "Jerry");//Normal COMMIT Transaction Con.commit ();} catch (Exception e) {e.printstacktrace ();//If an exception occurs, rollback the transaction try{con.rollback ();} catch (SQLException E1) {e1.printstacktrace ();}} Finally{jdbctools.releaseresource (Con,null,null);}}

JDBC Basic Learning (iv)-Database transactions

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.