我了解到MySQL(InnoDB)中的事务是原子性的,但当我在5个线程中测试下面的代码时,它们选择相同的ID:
$db->beginTransaction();
$row = $db->fetchRow("SELECT * FROM atomic WHERE selected = 0 LIMIT 1");
sleep(5);
$db->update("atomic", array('selected' => 1), "id = " . $row['id']);
$db->commit();
echo "Selected row: " . $row['id'];