0

Suppose I have an update query such as:

UPDATE accountstable SET bal = bal - 0.5 where bal >= 0.5 and id = 1

which subtracts 0.5 from an users account only if its balance is greater or equal to 0.5.

If the query is run twice, with both instances starting almost at the exact same time, will MySQL run both queries in parallel, or in a sequential/queued manner (waiting for the first instance to finish before running the second)?

If it is run in a sequential manner, would an account with a balance of 0.5, have a balance of 0 or -0.5 after the two queries complete?

Oo Dee
  • 145
  • 1
  • 11

1 Answers1

0

It's a race condition :)... Have a read of this on how to combat it:

https://dba.stackexchange.com/questions/61603/how-to-query-and-increase-a-value-counter-in-a-thread-safe-way-avoid-race-co

In a nutshell, you can protect against it using transactions.

Community
  • 1
  • 1
Brian
  • 8,418
  • 2
  • 25
  • 32