This commit is contained in:
2023-04-02 00:34:55 +08:00
parent 437e52896e
commit b4e9e204bc
2 changed files with 30 additions and 28 deletions
+6 -8
View File
@@ -99,15 +99,11 @@ class Connection extends Component
if (!$this->pool->hasChannel($config['cds'])) {
$this->pool->initConnections($config['cds'], $config['pool']['max']);
}
if (Context::hasContext($config['cds'])) {
if (!Context::hasContext($config['cds'])) {
return Context::setContext($config['cds'], $this->pool->get($config['cds'], $this->generate($config)));
} else {
return Context::getContext($config['cds']);
}
if ($this->pool->hasItem($config['cds'])) {
$connect = $this->pool->get($config['cds']);
} else {
$connect = $this->generate($config);
}
return Context::setContext($config['cds'], $connect);
}
@@ -115,8 +111,9 @@ class Connection extends Component
* @param array $config
* @return Closure
*/
public function generate(array $config): \PDO
public function generate(array $config): Closure
{
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'], [
@@ -136,6 +133,7 @@ class Connection extends Component
$link->beginTransaction();
}
return $link;
};
}
+6 -2
View File
@@ -155,9 +155,13 @@ class Pool extends Component
* @throws ConfigException
* @throws Exception
*/
public function get($name): mixed
public function get($name, $callback): mixed
{
return $this->channel($name)->pop();
$channel = $this->channel($name);
if (!$channel->isEmpty()) {
return $channel->pop();
}
return $callback();
}