Skip to content

Commit 688bdb3

Browse files
fix: keep writes transactional after MySQL deadlock 1213 (#10651)
1 parent ef9f864 commit 688bdb3

4 files changed

Lines changed: 390 additions & 2 deletions

File tree

‎system/Database/MySQLi/Connection.php‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ class Connection extends BaseConnection
9191
*/
9292
public $foundRows = false;
9393

94+
/**
95+
* InnoDB errno 1213 ended this transaction.
96+
*/
97+
private bool $transServerRolledBack = false;
98+
99+
/**
100+
* The next statement must BEGIN before it runs.
101+
*/
102+
private bool $transNeedsServerRestart = false;
103+
94104
/**
95105
* Connect to the database.
96106
*
@@ -291,6 +301,18 @@ protected function execute(string $sql)
291301
}
292302

293303
try {
304+
// 1213 ended the server transaction. BEGIN again so this statement
305+
// does not autocommit. A failed BEGIN stays pending. 1205 is unchanged:
306+
// innodb_rollback_on_timeout decides that case.
307+
if ($this->connID instanceof mysqli && $this->serverTransactionRestartPending()) {
308+
// Exception reporting off: a failed BEGIN returns false.
309+
if (! $this->connID->begin_transaction()) {
310+
return false;
311+
}
312+
313+
$this->transNeedsServerRestart = false;
314+
}
315+
294316
return $this->connID->query($this->prepQuery($sql), $this->resultMode);
295317
} catch (mysqli_sql_exception $e) {
296318
log_message('error', "{message}\nin {exFile} on line {exLine}.\n{trace}", [
@@ -300,6 +322,10 @@ protected function execute(string $sql)
300322
'trace' => render_backtrace($e->getTrace()),
301323
]);
302324

325+
if ($e->getCode() === 1213 && $this->transDepth > 0) {
326+
$this->markServerRolledBackTransaction();
327+
}
328+
303329
if ($this->DBDebug) {
304330
throw new DatabaseException($e->getMessage(), $e->getCode(), $e);
305331
}
@@ -574,16 +600,53 @@ public function insertID(): int
574600

575601
protected function _transBegin(): bool
576602
{
577-
return $this->connID->begin_transaction();
603+
if (! $this->connID->begin_transaction()) {
604+
return false;
605+
}
606+
607+
$this->clearDeadlockTransactionState();
608+
609+
return true;
578610
}
579611

580612
protected function _transCommit(): bool
581613
{
614+
// mysqli::commit() returns true after 1213. End the transaction
615+
// and report failure instead.
616+
if ($this->transServerRolledBack) {
617+
$this->transRollback();
618+
619+
return false;
620+
}
621+
582622
return $this->connID->commit();
583623
}
584624

585625
protected function _transRollback(): bool
586626
{
587-
return $this->connID->rollback();
627+
if (! $this->connID->rollback()) {
628+
return false;
629+
}
630+
631+
$this->clearDeadlockTransactionState();
632+
633+
return true;
634+
}
635+
636+
private function markServerRolledBackTransaction(): void
637+
{
638+
$this->transServerRolledBack = true;
639+
$this->transNeedsServerRestart = true;
640+
}
641+
642+
private function serverTransactionRestartPending(): bool
643+
{
644+
return $this->transNeedsServerRestart && $this->transDepth > 0;
645+
}
646+
647+
private function clearDeadlockTransactionState(): void
648+
{
649+
$this->transServerRolledBack = false;
650+
$this->transNeedsServerRestart = false;
588651
}
589652
}

0 commit comments

Comments
 (0)