From 8d277255832cbcf0a4daa1ee21d6147606fec514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Sun, 2 Apr 2023 00:16:25 +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 --- kiri-engine/Pool/Connection.php | 46 +++++++++++++++++---------------- kiri-engine/Pool/Pool.php | 8 ++---- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/kiri-engine/Pool/Connection.php b/kiri-engine/Pool/Connection.php index 4c443b45..8aca99f1 100644 --- a/kiri-engine/Pool/Connection.php +++ b/kiri-engine/Pool/Connection.php @@ -99,7 +99,11 @@ class Connection extends Component if (!$this->pool->hasChannel($config['cds'])) { $this->pool->initConnections($config['cds'], $config['pool']['max']); } - return $this->pool->get($config['cds'], $this->generate($config)); + + if (!$this->pool->hasItem($config['cds'])) { + return $this->generate($config); + } + return $this->pool->get($config['cds']); if (!Context::hasContext($config['cds'])) { return Context::setContext($config['cds'], ); } else { @@ -112,29 +116,27 @@ class Connection extends Component * @param array $config * @return Closure */ - public function generate(array $config): Closure + public function generate(array $config): \PDO { - return static function () use ($config) { - Kiri::getDi()->get(Kiri\Error\StdoutLoggerInterface::class)->alert('create database connect(' . $config['cds'] . ')'); + Kiri::getDi()->get(Kiri\Error\StdoutLoggerInterface::class)->alert('create database connect(' . $config['cds'] . ')'); - $link = new \PDO('mysql:dbname=' . $config['dbname'] . ';host=' . $config['cds'], $config['username'], $config['password'], [ - \PDO::ATTR_EMULATE_PREPARES => false, - \PDO::ATTR_CASE => \PDO::CASE_NATURAL, - \PDO::ATTR_PERSISTENT => true, - \PDO::ATTR_TIMEOUT => $config['connect_timeout'], - \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ($config['charset'] ?? 'utf8mb4') - ]); - $link->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); - $link->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false); - $link->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_EMPTY_STRING); - foreach ($config['attributes'] as $key => $attribute) { - $link->setAttribute($key, $attribute); - } - if (Db::inTransactionsActive()) { - $link->beginTransaction(); - } - return $link; - }; + $link = new \PDO('mysql:dbname=' . $config['dbname'] . ';host=' . $config['cds'], $config['username'], $config['password'], [ + \PDO::ATTR_EMULATE_PREPARES => false, + \PDO::ATTR_CASE => \PDO::CASE_NATURAL, + \PDO::ATTR_PERSISTENT => true, + \PDO::ATTR_TIMEOUT => $config['connect_timeout'], + \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ($config['charset'] ?? 'utf8mb4') + ]); + $link->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + $link->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false); + $link->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_EMPTY_STRING); + foreach ($config['attributes'] as $key => $attribute) { + $link->setAttribute($key, $attribute); + } + if (Db::inTransactionsActive()) { + $link->beginTransaction(); + } + return $link; } diff --git a/kiri-engine/Pool/Pool.php b/kiri-engine/Pool/Pool.php index bc03f900..209b5e83 100644 --- a/kiri-engine/Pool/Pool.php +++ b/kiri-engine/Pool/Pool.php @@ -155,13 +155,9 @@ class Pool extends Component * @throws ConfigException * @throws Exception */ - public function get($name, $callback): mixed + public function get($name): mixed { - $channel = $this->channel($name); - if ($channel->isEmpty()) { - $channel->push($callback()); - } - return $channel->pop(); + return $this->channel($name)->pop(); }