Understanding pdo mysql transactions

后端 未结 3 1247
盖世英雄少女心
盖世英雄少女心 2020-11-30 13:59

The PHP Documentation says:

If you\'ve never encountered transactions before, they offer 4 major features: Atomicity, Consistency, Isolation and Durabili

3条回答
  •  长情又很酷
    2020-11-30 14:04

    Alas, the "without interference" needs some help from the programmer. It needs BEGIN and COMMIT to define the extent of the 'transaction'. And...

    Your example is inadequate. The first statement needs SELECT ... FOR UPDATE. This tells the transaction processing that there is likely to be an UPDATE coming for the row(s) that the SELECT fetches. That warning is critical to "preventing interference". Now the timeline reads:

    • script1.php BEGINs
    • script2.php BEGINs
    • script1.php selects data (FOR UPDATE)
    • script2.php selects data is blocked, so it waits
    • script1.php updates data
    • script1.php commit() happens
    • script2.php selects data (and will get the newly-committed value)
    • script2.php updates data
    • script2.php commit() happens

    (Note: This is not a 'deadlock', just a 'wait'.)

提交回复
热议问题