When database transactions are used, there are still negative records in the concurrent state.

Source: Internet
Author: User
When a database transaction is used, the number of records in the concurrent state is still negative. for example, if a prize is killed in seconds, the number of prizes is 1, and five people send requests simultaneously, after a request is successful, the database value is changed to 0, next, perform other subsequent operations, and then submit the transaction. when the other four requests are sent, a message indicating that the boutique has been sent

Code

 

Now, if the number of prizes is changed to negative after the transaction is submitted and concurrent requests are executed after all operations are processed, if the number of prizes is updated, the transaction is submitted, for other operations, when another transaction is started and concurrent requests are made, the number of prize updates will not be negative. However, if this is done, after other operations are completed, if a failure occurs, you need to manually roll back the number of previously updated prizes. could you please tell us why the transaction processing logic is too long? if so, how to use transactions for long business logic


Reply to discussion (solution)

Mysql automatically commits transactions by default, that is, each SQL command is COMMIT
You need to take the initiative to close this line

During transaction processing, the related SQL commands are isolated in one copy, so they are relatively independent.
However, this does not mean that other connections cannot perform data operations.

Therefore, transactions can only guarantee the completeness of the operations in this link, and cannot control the operations of other connections.

Transaction start counter minus one if counter is negative: roll back, quit other operations commit


In fact, no transaction is used.
If the counter is negative, add one to the counter and exit other operations.

Mysql automatically commits transactions by default, that is, each SQL command is COMMIT
You need to take the initiative to close this line

During transaction processing, the related SQL commands are isolated in one copy, so they are relatively independent.
However, this does not mean that other connections cannot perform data operations.

Therefore, transactions can only guarantee the completeness of the operations in this link, and cannot control the operations of other connections.

Transaction start counter minus one if counter is negative: roll back, quit other operations commit


In fact, no transaction is used.
If the counter is negative, add one to the counter and exit other operations.



The counter here is about the number of prizes in the database, right?
If yes

So did I.
Start transaction
If the number is negative, roll back and exit. Otherwise, the number is-1.
Other operations
Submit


I used curl_multi_init to simulate concurrent requests. for example, if the number of remaining 1 simulated requests is 5 times, the number is-4.

Yes, but you did.
If (prize quantity <= 0 ){
Commit transactions
Return;
}
Prize quantity-1;
* Other Subsequent operations

Prize quantity-1;And you didn't say you want to determine his legitimacy.

Yes, but you did.
If (prize quantity <= 0 ){
Commit transactions
Return;
}
Prize quantity-1;
* Other Subsequent operations

Prize quantity-1;And you didn't say you want to determine his legitimacy.



This judgment

If (prize quantity-1 <0 ){
Roll back and exit
}
Prize quantity-1

Concurrent requests will still be reduced to negative numbers

The number of prizes-1 can be determined if it is less than 0 after the occurrence

Update tbl_name set prize = prize-1
Select prize from tbl_name
If prize <0
ROOLBACK
Endif

The number of prizes-1 can be determined if it is less than 0 after the occurrence

Update tbl_name set prize = prize-1
Select prize from tbl_name
If prize <0
ROOLBACK
Endif



Thank you for your patience. after the problem is solved, the deduction of concurrency cannot be changed to a negative number. I used to think that the effect is the same after the first judgment and then the first subtraction.

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.