From ed241646807d09f9994f9b455444b6baf695214f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Mon, 13 Feb 2023 14:27:28 +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 | 4 +++- Connection.php | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Command.php b/Command.php index 89e838b..94de022 100644 --- a/Command.php +++ b/Command.php @@ -157,7 +157,7 @@ class Command extends Component return $result == 0 ? true : $result; } catch (\PDOException|\Throwable $throwable) { if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { - Context::remove('master:client'); + $this->db->restore(true); unset($pdo); return $this->_execute(); @@ -187,6 +187,8 @@ class Command extends Component return $statement; } catch (\PDOException|\Throwable $throwable) { if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { + $this->db->restore(false); + unset($pdo); return $this->queryPrev($this->db->getSlaveClient()); } diff --git a/Connection.php b/Connection.php index 594928b..db2c80f 100644 --- a/Connection.php +++ b/Connection.php @@ -253,6 +253,17 @@ class Connection extends Component } + public function restore($isMaster) + { + if ($isMaster) { + Context::remove($this->cds . 'master'); + } else { + $slave = ($this->slaveConfig['cds'] ?? $this->cds) . 'slave'; + Context::remove($slave); + } + } + + /** * * 回收链接