Skip to content

Commit 873cd94

Browse files
committed
ACP2E-2345: MySQL has gone away error if consumer max_messages = 0
1 parent bc1c7ba commit 873cd94

File tree

1 file changed

+30
-13
lines changed
  • lib/internal/Magento/Framework/DB/Adapter/Pdo

1 file changed

+30
-13
lines changed

lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,13 @@ public function beginTransaction()
328328
}
329329
if ($this->_transactionLevel === 0) {
330330
$this->logger->startTimer();
331-
parent::beginTransaction();
332-
$this->logger->logStats(LoggerInterface::TYPE_TRANSACTION, 'BEGIN');
331+
try {
332+
$this->performQuery(function () {
333+
parent::beginTransaction();
334+
});
335+
} finally {
336+
$this->logger->logStats(LoggerInterface::TYPE_TRANSACTION, 'BEGIN');
337+
}
333338
}
334339
++$this->_transactionLevel;
335340
return $this;
@@ -598,9 +603,30 @@ protected function _checkDdlTransaction($sql)
598603
* @return \Zend_Db_Statement_Pdo|void
599604
* @throws Zend_Db_Adapter_Exception To re-throw \PDOException.
600605
* @throws Zend_Db_Statement_Exception
601-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
602606
*/
603607
protected function _query($sql, $bind = [])
608+
{
609+
$result = null;
610+
try {
611+
$this->_checkDdlTransaction($sql);
612+
$this->_prepareQuery($sql, $bind);
613+
$this->logger->startTimer();
614+
$result = $this->performQuery(fn () => parent::query($sql, $bind));
615+
} finally {
616+
$this->logger->logStats(LoggerInterface::TYPE_QUERY, $sql, $bind, $result);
617+
}
618+
619+
return $result;
620+
}
621+
622+
/**
623+
* Execute query and reconnect if needed.
624+
*
625+
* @param callable $queryExecutor
626+
* @return \Zend_Db_Statement_Pdo|void
627+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
628+
*/
629+
private function performQuery(callable $queryExecutor)
604630
{
605631
$connectionErrors = [
606632
2006, // SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
@@ -609,22 +635,15 @@ protected function _query($sql, $bind = [])
609635
$triesCount = 0;
610636
do {
611637
$retry = false;
612-
$this->logger->startTimer();
613638
try {
614-
$this->_checkDdlTransaction($sql);
615-
$this->_prepareQuery($sql, $bind);
616-
$result = parent::query($sql, $bind);
617-
$this->logger->logStats(LoggerInterface::TYPE_QUERY, $sql, $bind, $result);
618-
return $result;
639+
return $queryExecutor();
619640
} catch (\Exception $e) {
620641
// Finalize broken query
621642
$profiler = $this->getProfiler();
622643
if ($profiler instanceof Profiler) {
623-
/** @var Profiler $profiler */
624644
$profiler->queryEndLast();
625645
}
626646

627-
/** @var $pdoException \PDOException */
628647
$pdoException = null;
629648
if ($e instanceof \PDOException) {
630649
$pdoException = $e;
@@ -641,12 +660,10 @@ protected function _query($sql, $bind = [])
641660
$retry = true;
642661
$triesCount++;
643662
$this->closeConnection();
644-
645663
$this->_connect();
646664
}
647665

648666
if (!$retry) {
649-
$this->logger->logStats(LoggerInterface::TYPE_QUERY, $sql, $bind);
650667
$this->logger->critical($e);
651668
// rethrow custom exception if needed
652669
if ($pdoException && isset($this->exceptionMap[$pdoException->errorInfo[1]])) {

0 commit comments

Comments
 (0)