Question

I'm currently working on a PHP web app and using MySQL (xampp default settings) as my dbms. I'm just wondering what happens if there are two or more clients issue an UPDATE query to the database at the exact same time?

For example 

**User 1 issue**
UPDATE info SET reply='helloworldOne' WHERE info_id = 1; 

**User 2 issue**
UPDATE info SET reply='helloworldTwo' WHERE info_id = 1; 

**User 3 issue**
UPDATE info SET reply='helloworldThree' WHERE info_id = 1;

and so on....

Codes in my web app
$postReply = $_POST['reply'];

$update = "UPDATE info SET reply='$postReply' WHERE info_id = 1;";
$result = mysqli_query($connection, $update);

1) What happens in this case since all UPDATE query is on info_id 1?
2) Does MySQL automatically apply locking to info_id 1? As I did not apply any transactional codes (e.g. beginTransaction) into my PHP.
3) Which one would MySQL choose to update? And what happens to other clients?

Was it helpful?

Solution

Locking is done automatically when you do an update so basically the queries are not happening in the same moment, they will be queued one after another. The server will handle the race condition, so you can not predict the final result in your example.

OTHER TIPS

MySQL dbms handles the issue. Nothing to worry

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top