PHP/MySQL Critical section -
i'm using php pdo , innodb tables.
i want code allow 1 user-submitted operation complete, user can either cancel or complete. in case user posts both operations, want 1 of requests fail , rollback, isn't happening right now, both completing without exception/error. thought deleting row after checking exists enough.
$pdo = new pdo(); try { $pdo->begintransaction(); $rowcheck = $pdo->query("select * table id=99")->rowcount(); if ($rowcheck == 0) throw new runtimeexception("row isn't there"); $pdo->exec("delete table id = 99"); // either cancel, 1 bunch of queries. if (isset($_post['cancel'])) ... // or complete, bunch of queries. if (isset($_post['complete'])) ... // bunch of queries on other tables here... $pdo->commit(); } catch (exception $e) { $pdo->rollback(); throw $e; } how can make cancel / complete operations critical section? second operation must fail.
the code fine, 1 exception: add for update initial select. should suffice block second button press until first delete has happened, thereby leading second 1 "failing".
https://dev.mysql.com/doc/refman/5.5/en/innodb-locking-reads.html
note locking of rows update using
select updateapplies when autocommit disabled (either beginning transactionstart transactionor setting autocommit 0. if autocommit enabled, rows matching specification not locked.
Comments
Post a Comment