Answer the question
In order to leave comments, you need to log in
Can 2 queries run at the same time?
There is a piece of function code:
var updater = function() {
mysql.query('SELECT balance FROM Users WHERE id = 5').then(function(balance){
if(balance < 100) {
return;
}
mysql.query('UPDATE Users SET balance = balance - 100 WHERE id = 5').then(function(res){
return;
});
});
}
Answer the question
In order to leave comments, you need to log in
And why not write one request and not take a steam bath about whether it will have time to be executed there or not? Something like this:
UPDATE Users SET
balance = balance - 100
WHERE id = 5 AND balance >= 100
yes, such a situation may be, in your case you have already been offered a solution by simplifying the query, but there are other cases in which it is worth using sql transactions, correctly hanging unique indexes so that there are no duplicates with conflicting queries
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question