This commit is contained in:
2023-04-02 00:16:25 +08:00
parent 314bc98044
commit 8d27725583
2 changed files with 26 additions and 28 deletions
+6 -4
View File
@@ -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,9 +116,8 @@ 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'] . ')');
$link = new \PDO('mysql:dbname=' . $config['dbname'] . ';host=' . $config['cds'], $config['username'], $config['password'], [
@@ -134,7 +137,6 @@ class Connection extends Component
$link->beginTransaction();
}
return $link;
};
}
+2 -6
View File
@@ -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();
}