您有以下兩列一個InnoDB表答:ID(PK),狀態MySQL的InnoDB的併發更新
兩個線程訪問以下順序表:
Thread 1: SELECT id from A where status = 0 LIMIT 1;
Thread 2: SELECT id from A where status = 0 LIMIT 1;
兩個線程選擇ID 1.
Thread 1: UPDATE A SET status = 1 WHERE id = 1 AND status = 0;
Thread 2: UPDATE A SET status = 2 WHERE id = 1 AND status = 0;
是否有可能兩個線程更新同一行?
現在增加交易:
Thread 1: SELECT id from A where status = 0 LIMIT 1;
Thread 2: SELECT id from A where status = 0 LIMIT 1;
Thread 1: START TRANSACTION;
Thread 2: START TRANSACTION;
Thread 1: UPDATE A SET status = 1 WHERE id = 1 AND status = 0;
Thread 2: UPDATE A SET status = 2 WHERE id = 1 AND status = 0;
Thread 1: COMMIT;
Thread 2: COMMIT;
是現在可能兩個線程將更新行?
在任何一種情況下,如果兩行都可以更新,那麼解決這個問題的正確方法是什麼?
可能重複[如何處理數據庫中的併發更新?](http://stackoverflow.com/questions/1195858/how-to-deal-with-concurrent-updates-in-databases) –