Skip to content

EspoCRM – Open Source CRM Application

Deadlock proper handling savepoints

Yurii824670fd6d7c
diff --git a/application/Espo/ORM/Executor/DefaultSqlExecutor.php b/application/Espo/ORM/Executor/DefaultSqlExecutor.php
index d56ef9ef9a..6870ddd307 100644
--- a/application/Espo/ORM/Executor/DefaultSqlExecutor.php
+++ b/application/Espo/ORM/Executor/DefaultSqlExecutor.php
@@ -125,6 +125,10 @@ class DefaultSqlExecutor implements SqlExecutor
return true;
}
+ if ($state === 'HY000' && (int) $e->errorInfo[1] === 1020) {
+ return true;
+ }
+
return $state === '40001' && (int) $e->errorInfo[1] === 1213;
}
}
diff --git a/application/Espo/ORM/TransactionManager.php b/application/Espo/ORM/TransactionManager.php
index 601045ed5d..3efb4d3430 100644
--- a/application/Espo/ORM/TransactionManager.php
+++ b/application/Espo/ORM/TransactionManager.php
@@ -80,6 +80,8 @@ class TransactionManager
{
$counter ??= self::MAX_ATTEMPT_COUNT;
+ $isOutermost = $this->level === 0;
+
$this->start();
try {
@@ -87,7 +89,16 @@ class TransactionManager
$this->commit();
} catch (DeadlockException $e) {
+ // The entire transaction is aborted, including save points.
+
$this->level --;
+
+ if (!$isOutermost) {
+ throw $e;
+ }
+
+ $this->forceRollback();
+
$counter --;
if ($counter === 0) {
@@ -189,4 +200,13 @@ class TransactionManager
$this->pdo->exec($sql);
}
+
+ private function forceRollback(): void
+ {
+ try {
+ if ($this->pdo->inTransaction()) {
+ $this->pdo->rollBack();
+ }
+ } catch (PDOException) {}
+ }
}