php - Does MySQL queue queries? -


what happens if there 2 people sending same query @ same time database , 1 makes other query return different?

i have shop there 1 item left. 2 or more people buy item , query arrives @ exact same time on mysql server. guess queue if so, how mysql pick first 1 execute , can have influence on this?

sending same query @ same time

queries not run in parallel

it depends on database engine. myisam, every query acquires table level lock meaning queries run sequentially queue. of other engines may run in parallel.

echo_me says nothing happens @ exact same time , cpu not @ once

that's not true. it's possible dbms run on machine more 1 cpu, , more 1 network interface. it's very improbable 2 queries arrive @ same time - not impossible, hence there mutex ensure paring/execution transition runs single thread (of execution - not necesarily same light weight process).

there's 2 approaches solving concurent dml - either use transactions (where each user gets clone of database) , when queries have completed dbms tries reconcile changes - if reconciliation fails, dbms rolls 1 of queries , reports failed. other approach use row-level locking - dbms identifies rows updated query , marks them reserved update (other users can read original version of each row attempt update data blocked until row available again).

your problem have 2 mysql clients, each of have retrieved fact there 1 item of stock left. further complicated fact (since mention php) stock levels may have been retrieved in different dbms session subsequent stock adjustment - cannot have transaction spanning more http request. hence need revalidate fact maintained outside dbms within single transaction.

optimistic locking can create pseudo - transaction control mechanism - flag record modify timestamp , user identifier (with php php session id choice) - if when come modify it, else has changed it, code knows data retrieved invalid. can lead other complications.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -