This commit is contained in:
2021-02-15 20:45:35 +08:00
parent b862e828fc
commit 74604324b1
3 changed files with 22 additions and 31 deletions
+11 -9
View File
@@ -26,7 +26,6 @@ class Connection extends Pool
protected array $connections = [];
/**
* @param $timeout
*/
@@ -151,13 +150,12 @@ class Connection extends Pool
if (Context::hasContext($coroutineName)) {
return Context::getContext($coroutineName);
}
if ($this->size($coroutineName) < 1) {
$connections = $this->newClient($config, $coroutineName);
} else {
[$time, $connections] = $this->get($coroutineName);
if (!($connections instanceof PDO)) {
throw new Exception('Database exception.');
}
if (!$this->hasItem($coroutineName)) {
return Context::setContext($coroutineName, $this->newClient($config, $coroutineName));
}
[$time, $connections] = $this->get($coroutineName);
if (!($connections instanceof PDO)) {
throw new Exception('Database exception.');
}
return Context::setContext($coroutineName, $connections);
}
@@ -172,7 +170,7 @@ class Connection extends Pool
private function newClient($config, $coroutineName): PDO|null
{
$this->printClients($config['cds'], $coroutineName, true);
return $this->createConnect($this->parseConfig($config, $coroutineName), $coroutineName, function ($cds, $username, $password, $charset, $coroutineName) {
$connections = $this->createConnect($this->parseConfig($config, $coroutineName), $coroutineName, function ($cds, $username, $password, $charset, $coroutineName) {
$link = new PDO($cds, $username, $password, [
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_CASE => PDO::CASE_NATURAL,
@@ -193,6 +191,10 @@ class Connection extends Pool
}
return $link;
});
if ($connections === false) {
return $this->newClient($config, $coroutineName);
}
return $connections;
}