diff --git a/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php b/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php index 1f385b18f867..26bb41447c5a 100644 --- a/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php +++ b/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php @@ -294,8 +294,13 @@ public function beginTransaction() } if ($this->_transactionLevel === 0) { $this->logger->startTimer(); - parent::beginTransaction(); - $this->logger->logStats(LoggerInterface::TYPE_TRANSACTION, 'BEGIN'); + try { + $this->performQuery(function () { + parent::beginTransaction(); + }); + } finally { + $this->logger->logStats(LoggerInterface::TYPE_TRANSACTION, 'BEGIN'); + } } ++$this->_transactionLevel; return $this; @@ -546,9 +551,30 @@ protected function _checkDdlTransaction($sql) * @return \Zend_Db_Statement_Pdo|void * @throws Zend_Db_Adapter_Exception To re-throw \PDOException. * @throws Zend_Db_Statement_Exception - * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _query($sql, $bind = []) + { + $result = null; + try { + $this->_checkDdlTransaction($sql); + $this->_prepareQuery($sql, $bind); + $this->logger->startTimer(); + $result = $this->performQuery(fn () => parent::query($sql, $bind)); + } finally { + $this->logger->logStats(LoggerInterface::TYPE_QUERY, $sql, $bind, $result); + } + + return $result; + } + + /** + * Execute query and reconnect if needed. + * + * @param callable $queryExecutor + * @return \Zend_Db_Statement_Pdo|void + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + */ + private function performQuery(callable $queryExecutor) { $connectionErrors = [ 2006, // SQLSTATE[HY000]: General error: 2006 MySQL server has gone away @@ -557,22 +583,15 @@ protected function _query($sql, $bind = []) $triesCount = 0; do { $retry = false; - $this->logger->startTimer(); try { - $this->_checkDdlTransaction($sql); - $this->_prepareQuery($sql, $bind); - $result = parent::query($sql, $bind); - $this->logger->logStats(LoggerInterface::TYPE_QUERY, $sql, $bind, $result); - return $result; + return $queryExecutor(); } catch (\Exception $e) { // Finalize broken query $profiler = $this->getProfiler(); if ($profiler instanceof Profiler) { - /** @var Profiler $profiler */ $profiler->queryEndLast(); } - /** @var $pdoException \PDOException */ $pdoException = null; if ($e instanceof \PDOException) { $pdoException = $e; @@ -589,12 +608,10 @@ protected function _query($sql, $bind = []) $retry = true; $triesCount++; $this->closeConnection(); - $this->_connect(); } if (!$retry) { - $this->logger->logStats(LoggerInterface::TYPE_QUERY, $sql, $bind); $this->logger->critical($e); // rethrow custom exception if needed if ($pdoException && isset($this->exceptionMap[$pdoException->errorInfo[1]])) {