Ich bin kürzlich auf 1020-Fehler „Datensatz hat sich seit dem letzten Einlesen der Tabelle geändert“ bei Code gestoßen, der 20 Jahre lang ohne diesen Fehler ausgeführt wurde. Es ist kaum zu glauben, dass sich der „Datensatz seit dem letzten Einlesen in der Tabelle geändert hat“, da die fehlgeschlagene SQL-Abfrage nur für Datensätze gilt, die von einem Benutzer (Besucher) verwendet werden:
Code: Select all
DELETE FROM vbshoppingcart where visitor_id = :visitor_id AND businessunitID = :businessunitID
Code: Select all
$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());
}
Mobile version