Description and functions of conn. setautocommit ()

Source: Internet
Author: User

Transaction operation data:

(1)

Eg:

Assume that the Bank Transfer System

Transfer from Michael Jacob's account to Mr. Luo's account of 1000 yuan
This operation takes two steps.
1. Michael's account is reduced by 1000 RMB
2. Mr. Luo's account was increased by 1000 yuan.
For example, when the power is cut off and so on, but Michael dropped by 500, then Mr. Luo did not increase .... I can imagine that the Bank has something to do now.

Therefore, you can use the transaction operation data to handle the problem.

 

(2)

Java is very simple to use transactions. First, call Conn. setautocommit (Boolean B) method. If it is set to false, it will not be submitted automatically. Instead, you must use Conn. the COMMIT () method is used to manually commit transactions. Of course, the transaction can be committed only when the two steps are confirmed to have no errors. In this way, the integrity of the entire operation can be ensured. If an error occurs, use Conn. the rollback () method to roll back the transaction. In this way, the entire transaction will not be committed. So how can we determine whether there are any errors? It is very simple. java. SQL. sqlexception will be thrown when we execute database operations, so try ...... Catch Block catch exception. In the Catch Block, use conn. rollback () to roll back the transaction.
In the JavaBean called by the database, the conn. setautocommit () function is used as a transaction commit every time an SQL statement is executed. However, in a project, you may need to execute multiple SQL statements as a transaction. If one execution fails, rollback () is returned ();

Generally, you can add, delete, modify, and query tables in the database, when a business logic requires two or more operations to add, delete, modify, and delete the same data table, in order to ensure data consistency (or specifically, in one of your business logic processing processes, other (thread or program or business processing) Same two queries for the same data have the same result .) We will use the transaction operations of the database to encapsulate all operations contained in the business logic in one transaction and commit multiple operations at a time.
The setautocommit method of conn refers to whether to automatically submit a statement once in a transaction.
You want to perform multiple operations in a transaction. The setautocommit parameter must be set to false. The conn. Commit () method is called at the end of multiple operations for manual submission.

(3) parameters: true and false

Assume that:
1. A database table has 50 records
2. Set the parameter to false.
When you execute the entire SQL query, there is no transaction, so if you use some functions for your query, these functions contain multiple query statements, then there may be inconsistencies.
That is to say, functions and stored procedures will all run in non-transactional environments.
If you set it to true, there will be no problems, and read consistency will ensure that there will be no problems.

So it is usually set to true, as I do.

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.