Mysql ------ transaction

Source: Internet
Author: User

Mysql ------ transaction

Transactions

I. Concepts

Database Transaction refers to a series of operations performed as a single logical unit of work, either completely or completely. Transaction Processing ensures that data-oriented resources are not updated permanently unless all operations in the transaction unit are successfully completed. By combining a set of related operations into a unit that either succeeds or fails, you can simplify error recovery and make the application more reliable. (Transactions are the units in which databases maintain data consistency. Data Consistency can be maintained at the end of each transaction .)

For example, for bank transfers: If you deduct money from one account and add money to another account, either or both of these operations will be executed. This is a transaction.

II, Transaction statementStart TRANSACTION: BEGIN TRANSACTION
Submit TRANSACTION: COMMIT TRANSACTION
Rollback transaction: rollback transaction if an error occurs during the operation, you should start a new TRANSACTION. Iii. Attributes

1. atomicity: the statements that constitute Transaction Processing Form A logical unit and cannot only execute a part of them.
2. consistency: the database is consistent before and after transaction processing (database data integrity constraints ).
3. Isolation: the impact of one transaction processing on the processing of another transaction.
4. durability: the effect of transaction processing can be permanently saved.
5. A transaction has only one result: either successful or failed.

Iv. Differences between General SQL statements, batch processing, and transaction processing and java implementation code templates

(The following Connection con = ConnFactory. getConn () can be seen and click to open the link)

1. General SQL statements

If no transaction is used, one statement takes effect after passing one statement. If an exception occurs, none of the subsequent statements can be executed.

 

@ Testpublic void save1 () throws Exception {Connection con = ConnFactory. getConn (); Statement st = con.createstatement(;;st.exe cute ("insert into stud (id, sname, age) values (1010, 'yiyang 1', 25) "inserting into st.exe cute (" insert into stud (id, sname, age) values (1010, 'yiyang 2', 28) "inserting into st.exe cute (" insert into stud (id, sname, age) values (1011, 'yiyang 1', 25 )");}
2. Batch Processing

 

Transaction processing is not adopted. Batch Processing: all valid statements can be effectively executed. The statements with exceptions cannot be executed by themselves, but do not affect subsequent statements.

@ Testpublic void save2 () throws Exception {Connection con = ConnFactory. getConn (); Statement st = con. createStatement (); st. addBatch ("insert into stud (id, sname, age) values (1010, 'yiyang 1', 25)"); st. addBatch ("insert into stud (id, sname, age) values (1010, 'yiyang 2', 28)"); st. addBatch ("insert into stud (id, sname, age) values (1011, 'yiyang 3', 25)" inserting into st.exe cuteBatch ();}
3. Transaction Processing Template
@ Testpublic void save3 () throws Exception {Connection con = ConnFactory. getConn (); try {con. setAutoCommit (false); // The default value is true, that is, no Transaction is used. ---- false is equivalent to "start Transaction" Statement st = con.createStatement();st.exe cute ("insert into stud (id, sname, age) values (1010, 'yiyang 1', 25) "inserting into st.exe cute (" insert into stud (id, sname, age) values (1010, 'yiyang 2', 28) "Maid (" insert into stud (id, sname, age) values (1012, 'yiyang 3', 25) "); con. commit ();} catch (Exception e) {System. out. println ("rolled back .... "); con. rollback ();} finally {con. setAutoCommit (true); // restores the scene and sets it to the default non-transaction processing state con. close ();}}

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.