From b1718a657f007272e9d2b4d292c0a10a37f4855f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=97=E5=B1=B1=E5=90=B4=E5=BD=A6=E7=A5=96?= Date: Sat, 16 Feb 2019 19:27:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BF=9E=E6=8E=A5=E6=B1=A0?= =?UTF-8?q?=E9=87=8A=E6=94=BE=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/LayerInterface.php | 11 +++++++++++ src/MySQL.php | 9 +++++++++ src/MySQL/Pool.php | 12 +++++++++++- src/PoolInterface.php | 12 +++++++++++- src/Redis.php | 9 +++++++++ src/Redis/Pool.php | 12 +++++++++++- 6 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/LayerInterface.php b/src/LayerInterface.php index 1bc532a..7d4b8d0 100644 --- a/src/LayerInterface.php +++ b/src/LayerInterface.php @@ -1,6 +1,17 @@ master($sql, $params, $timeout); } + + /** + * 释放数据连接 + */ + public function clean() + { + $this->masterPool->clean(); + $this->slavePool->clean(); + } } \ No newline at end of file diff --git a/src/MySQL/Pool.php b/src/MySQL/Pool.php index ff8048a..5c784e5 100644 --- a/src/MySQL/Pool.php +++ b/src/MySQL/Pool.php @@ -51,7 +51,7 @@ public function get() : Item $item = $this->pool->pop($this->timeout); if ($item === false) { - throw new Exception('Get Item instance timeout, all connection is used!'); + throw new Exception('Get mysql connection instance timeout, pool: ' . $this->getLength()); } return $item; @@ -64,4 +64,14 @@ public function getLength() : int { return $this->pool->length(); } + + /** + * 清空数据库连接池(释放数据连接) + */ + public function clean() + { + while ($item = $this->pool->pop($this->timeout)) { + $item->close(); + } + } } diff --git a/src/PoolInterface.php b/src/PoolInterface.php index ef75edf..66cb47f 100644 --- a/src/PoolInterface.php +++ b/src/PoolInterface.php @@ -1,7 +1,6 @@ slavePool->put($item); } + + /** + * 释放数据连接 + */ + public function clean() + { + $this->masterPool->clean(); + $this->slavePool->clean(); + } } diff --git a/src/Redis/Pool.php b/src/Redis/Pool.php index 23ee49b..f7d6591 100644 --- a/src/Redis/Pool.php +++ b/src/Redis/Pool.php @@ -51,7 +51,7 @@ public function get() $item = $this->pool->pop($this->timeout); if ($item === false) { - throw new Exception('Get Item instance timeout, all connection is used!'); + throw new Exception('Get redis connection instance timeout, pool: ' . $this->getLength()); } return $item; @@ -64,4 +64,14 @@ public function getLength() : int { return $this->pool->length(); } + + /** + * 清空数据库连接池(释放数据连接) + */ + public function clean() + { + while ($item = $this->pool->pop($this->timeout)) { + $item->close(); + } + } }