From f86d15505950474da635b8d4ac5f36c52081a074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Mon, 30 Jan 2023 11:03:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Command.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/Command.php b/Command.php index 9794981..89e838b 100644 --- a/Command.php +++ b/Command.php @@ -12,6 +12,7 @@ namespace Database; use Exception; use Kiri\Abstracts\Component; +use Kiri\Di\Context; use PDOStatement; /** @@ -151,20 +152,26 @@ class Command extends Component $result = (int)$pdo->lastInsertId(); $prepare->closeCursor(); - $result = $result == 0 ? true : $result; - } catch (\Throwable $exception) { - $result = $this->logger->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql'); - } finally { $this->db->release($pdo, true); - return $result; + + return $result == 0 ? true : $result; + } catch (\PDOException|\Throwable $throwable) { + if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { + Context::remove('master:client'); + + unset($pdo); + return $this->_execute(); + } + + $this->db->release($pdo, true); + + return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql'); } } /** * @param \PDO $pdo - * @param string $sql - * @param array $params * @return PDOStatement * @throws Exception */