EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Email import: Handling snapshot isolation conflict, error 1020
Yuriif7ab4f7ffbe3
diff --git a/application/Espo/Core/Mail/Importer/DefaultImporter.php b/application/Espo/Core/Mail/Importer/DefaultImporter.phpindex de82eb8703..67c2ec3d0a 100644--- a/application/Espo/Core/Mail/Importer/DefaultImporter.php+++ b/application/Espo/Core/Mail/Importer/DefaultImporter.php@@ -66,11 +66,13 @@ use Espo\Tools\Stream\Jobs\ProcessNoteAcl;use DateTime;use Exception;+use PDOException;class DefaultImporter implements Importer{private const SUBJECT_MAX_LENGTH = 255;private const PROCESS_ACL_DELAY_PERIOD = '5 seconds';+ private const int SAVE_RETRY_COUNT = 2;/** @var AssignmentNotificator<Email> */private AssignmentNotificator $notificator;@@ -593,17 +595,34 @@ class DefaultImporter implements Importerprivate function processFinalTransactionalSave(Email $email): void{- $this->entityManager->getTransactionManager()->start();-- $this->entityManager- ->getRDBRepositoryByClass(Email::class)- ->forUpdate()- ->where([Attribute::ID => $email->getId()])- ->findOne();+ for ($i = 0; $i < self::SAVE_RETRY_COUNT; $i ++) {+ try {+ $this->processFinalTransactionalSaveInternal($email);+ } catch (PDOException $e) {+ $code = (int) ($e->errorInfo[1] ?? '0');++ // Handles a snapshot isolation conflict.+ if ($code === 1020) {+ continue;+ }- $this->entityManager->saveEntity($email, [Email::SAVE_OPTION_IS_BEING_IMPORTED => true]);+ throw $e;+ }+ }+ }- $this->entityManager->getTransactionManager()->commit();+ private function processFinalTransactionalSaveInternal(Email $email): void+ {+ $this->entityManager->getTransactionManager()->run(function () use ($email) {+ $this->entityManager+ ->getRDBRepositoryByClass(Email::class)+ ->forUpdate()+ ->select(Attribute::ID)+ ->where([Attribute::ID => $email->getId()])+ ->findOne();++ $this->entityManager->saveEntity($email, [Email::SAVE_OPTION_IS_BEING_IMPORTED => true]);+ });}/**