A piece of code executed 2 SQL, the implication is to let two SQL to echo, but today there are some problems, the great god see the content

Source: Internet
Author: User
Because there are a lot of database fields, I'll make an analogy.
Details table List
Users table user has a field num

I execute a method that inserts a single piece of data into a list at the same time to the num+1 of the user table

There were about 20W registered users last month, and I got the numbers from these users ' list tables separately, and compared them to num in the user table.

The comparison is about 70 people's list data, which is about 10 less than the number recorded by Num.

Because the methods are executed successively, presumably in the order of precedence
Public Function test () {
Insert $data;
Update num+1;
}

Insert in list first, update the User num field


I'd like to ask what happens when this data is out of sync. Please do not let the sticker code.


Reply to discussion (solution)

You're supposed to use the business.
Undo operation when insert $data or update num+1 fails


Did you use transaction processing? Determine if insert is successful before updating field
Insert $data; Failed but still executed update num+1; it will be inconsistent.

Did you use transaction processing? Determine if insert is successful before updating field
Insert $data; Failed but still executed update num+1; it will be inconsistent.



Eldest brother, generally under what circumstances will fail, feel no situation ah, is an INSERT statement, the user name is the mobile phone number. Fields can also be empty

Duplicate phone number, write value and field type inconsistency may fail
You should filter user data before you write to the database

Duplicate phone number, write value and field type inconsistency may fail
You should filter user data before you write to the database



Definitely filtered, that day wrote a monitoring, the results to now no problem, and so on to know what the situation, thanks buddies.
  • 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.