Недавно я столкнулся с ошибкой 1020 «Запись изменилась с момента последнего чтения в таблице» в коде, который работал без этой ошибки в течение 20 лет. Трудно поверить, что «запись изменилась с момента последнего чтения в таблице», поскольку неудачный SQL-запрос предназначен только для записей, используемых одним пользователем (посетителем):
Код: Выделить всё
DELETE FROM vbshoppingcart where visitor_id = :visitor_id AND businessunitID = :businessunitID
Код: Выделить всё
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
$this->dbh->beginTransaction();
// other queries within the same transaction will precede the query in question
$retry_attempts = 3;
$retry_wait_microseconds = 100000; // 100ms
while ($retry_attempts > 0) {
$this->stmt = $this->dbh->prepare($this->query);
$this->bindparams();
try {
$returnvalue = $this->stmt->execute();
$this->clearBindvalues();
return $returnvalue;
} catch (\PDOException $e) {
$error_code = $e->getCode();
$error_message = $e->getMessage();
if ($error_code === 'HY000' && strpos($error_message, '1020') !== false) {
// Retry in case of 1020
error_log("Retry because of error 1020 – remaining: $retry_attempts.");
usleep($retry_wait_microseconds);
$retry_attempts--;
continue;
}
// If no possibility that the query can be successfully executed clear the bind values in order to have the object ready for additional DB queries
$this->clearBindvalues();
}
throw new \Exception('PDO error occurred. Could not perform desired database interaction.');
}
// other queries within the same transaction will follow the query in question
try {
// At the end the transaction will be committed:
$result = $this->dbh->commit();
} catch (\PDOException $e) {
error_log("Commit failed: " . $e->getMessage());
$this->dbh->rollBack();
throw new \Exception("Transaction commit failed: " . $e->getMessage());
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... d-in-table
Мобильная версия