Two SQL statements are executed in a piece of code, which means that two SQL statements are to be matched, but some problems have occurred today. please refer to the content.

Source: Internet
Author: User
Two SQL statements are executed in a piece of code, which means two SQL statements are to be matched, but today there are some problems. please refer to the content. because there are many database fields, let me make an analogy.
Detailed information table list
The user table has a field num.

I am executing a method that inserts a piece of data into the list, and at the same time to the num + 1 of the user table

There were approximately 20 million registered users last month. I calculated the numbers in these user list tables and compared them with num in the user table.

The comparison result shows the number of list data records of approximately 70 persons, which is about 10 less than the number recorded by num.

This is because the methods are executed sequentially.
Public function test (){
Insert $ data;
Update num + 1;
}

Insert to list first, and then update the user num field


I would like to ask under what circumstances will this happen if the data is not synchronized. Please do not paste the code.


Reply to discussion (solution)

You should use transactions.
Undo the operation if insert $ data or update num + 1 fails.


Is Transaction processing used? Determine whether to update the field after successful insertion
Insert $ data; if it fails, but update num + 1 is still executed; then it will be inconsistent.

Is Transaction processing used? Determine whether to update the field after successful insertion
Insert $ data; if it fails, but update num + 1 is still executed; then it will be inconsistent.



Big Brother: generally, it will fail under any circumstances. it is an insert statement with the user name as the mobile phone number. Fields can also be empty.

The mobile phone number is repeated. if the write value and field type are inconsistent, the operation may fail.
You should filter user data before writing data to the database.

The mobile phone number is repeated. if the write value and field type are inconsistent, the operation may fail.
You should filter user data before writing data to the database.



It must have been filtered out. I wrote a monitoring report that day, but now there is no problem. when there is a problem, I will know what the problem is, thanks buddy.

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.