MySQL database transaction

Source: Internet
Author: User

Database transactions?
Logical work unit: the continuous changes in the system in a logical unit, can be executed all, can not be executed all.

Rollback of a transaction:

Database transaction, which is a series of operations performed as a single logical unit of work, Transaction
Either completely executed, or completely not executed. Transaction processing ensures that unless all operations within a transactional unit are completed successfully,
Otherwise, data-oriented resources will not be permanently updated. By combining a set of related actions into one or all of the successful
Either all failed units can simplify error recovery and make the application more reliable. A logical unit of work to be a transaction,
The so-called acid (atomicity, consistency, isolation, and durability) properties must be met.
A transaction is a logical unit of work in a database run, which is handled by the transaction management subsystem in the DBMS.

Why do databases open transactions?
1. If True then auto-commit. Auto Submit don't worry about the last question you want to submit,
It is automatically completed, and each statement is treated as a transaction;
Commit manually, all statements before you show the commit are considered a transaction,
The advantage is that when one of the statements in this transaction fails, the transaction is rolled back,
That is, they do not write to the database, which facilitates the consistency of the database.

2. The default is to commit transactions automatically, that is, each updated SQL statement to the database
Represents a transaction, successful operation
, the system automatically calls commit () to commit, or rollback () is called to revoke the transaction
When using JDBC transaction demarcation, multiple SQL statements can be combined into one transaction, and one of the JDBC transactions
The disadvantage is that the scope of a transaction is limited to one database connection, and a JDBC transaction cannot span multiple databases.

1. The JDBC transaction--------java.sql.Connection (JDBC Interface) provides two transaction modes, automatic commit and manual commit,

/* Auto-commit: When the record is updated, the system automatically commits, cannot maintain the consistency of the transaction, and cannot guarantee the data integrity.
Manual commit: It takes the transaction to be done by you, and in the event of an exception, the transaction can be rolled back, keeping the transaction consistent.
The following is a discussion of application differences in practice:
1. In JDBC, if the method Setautocommit (Boolean autocommit) of the connection class is assigned a value of false, then it can be manually committed (invoking the commit () method of the connection class);
2, if True then is automatic submission. Automatic submission you don't have to worry about the last issue to be submitted, he will automatically complete, and each statement is treated as a transaction;
3, manual submission, before you show the submission of all statements are considered a transaction, its advantage is that when one of the statements in this transaction fails, the transaction will be rolled back, that is, will not write to the database, which is conducive to maintaining the consistency of the database.
For example, when you insert related data both in the primary table and from the table, if you fail from the table (or the primary table), and the primary table (or the table) succeeds, the data is not written to the primary table (or from the table).

*/

Java.sqk.Connection provides the following methods for controlling transactions
public void Setautocommit (Boolean)

public boolean getautocommit ()

public void commit ()

public void rollback ()

MySQL database transaction

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.