This commit is contained in:
2023-08-29 22:13:37 +08:00
parent 5158b709bd
commit 91edb08238
2 changed files with 17 additions and 52 deletions
+8 -8
View File
@@ -77,8 +77,8 @@ class Command extends Component
*/
public function all(): bool|array
{
$client = $this->connection->getConnection();
try {
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]);
}
@@ -90,7 +90,7 @@ class Command extends Component
}
return $this->error($throwable);
} finally {
$this->connection->release($client);
$this->connection->release($client ?? null);
}
}
@@ -100,8 +100,8 @@ class Command extends Component
*/
public function one(): null|bool|array
{
$client = $this->connection->getConnection();
try {
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]);
}
@@ -113,7 +113,7 @@ class Command extends Component
}
return $this->error($throwable);
} finally {
$this->connection->release($client);
$this->connection->release($client ?? null);
}
}
@@ -123,8 +123,8 @@ class Command extends Component
*/
public function fetchColumn(): mixed
{
$client = $this->connection->getConnection();
try {
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]);
}
@@ -136,7 +136,7 @@ class Command extends Component
}
return $this->error($throwable);
} finally {
$this->connection->release($client);
$this->connection->release($client ?? null);
}
}
@@ -158,8 +158,8 @@ class Command extends Component
*/
private function _execute(): bool|int
{
$client = $this->connection->getConnection();
try {
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]);
}
@@ -178,7 +178,7 @@ class Command extends Component
}
return $this->error($throwable);
} finally {
$this->connection->release($client);
$this->connection->release($client ?? null);
}
}
+9 -44
View File
@@ -143,13 +143,13 @@ class Connection extends Component
$length = $pool->size($this->cds);
for ($i = 0; $i < $length; $i++) {
try {
if (($client = $this->pop($pool, false)) === false) {
if (($client = $this->validator($pool)) === false) {
break;
}
$pool->push($this->cds, [$client, time()]);
$pool->push($this->cds, $client);
} catch (\Throwable $exception) {
if (!str_contains($exception->getMessage(), 'Client timeout.')) {
$this->logger->error(throwable($exception));
$this->logger->error(throwable($exception), [$this->cds]);
}
$pool->abandon($this->cds);
}
@@ -159,25 +159,16 @@ class Connection extends Component
/**
* @param Pool $pool
* @param bool $isWaite
* @return PDO|bool
* @throws Exception
*/
protected function pop(Pool $pool, bool $isWaite): PDO|bool
protected function validator(Pool $pool): PDO|bool
{
if ($isWaite) {
$bool = $pool->get($this->cds, $this->waite_time);
} else {
$bool = $pool->get($this->cds);
}
if ($bool === false) {
if (($bool = $pool->get($this->cds)) === false) {
return false;
}
/** @var PDO $client */
[$client, $time] = $bool;
if ((time() - $time) > $this->idle_time) {
throw new Exception('Client timeout.');
}
if ($client->query('select 1') === false) {
throw new Exception($client->errorInfo()[1]);
}
@@ -223,37 +214,11 @@ class Connection extends Component
*/
protected function getNormalClientHealth(): PDO
{
try {
$data = $this->pop($this->pool(), true);
if ($data === false) {
throw new Exception('Pool waite timeout at ' . $this->waite_time);
}
return $data;
}catch (\Throwable $exception) {
$this->logger->error($exception->getMessage(), [$this->cds]);
$this->pool()->abandon($this->cds);
return $this->getNormalClientHealth();
}
}
/**
* @param PDO|null $client
* @return bool
*/
protected function canUse(?PDO $client): bool
{
if (is_null($client)) {
return false;
}
try {
if ($client->query('select 1') === false) {
return false;
}
return true;
} catch (\Throwable $exception) {
return false;
$data = $this->pool()->get($this->cds, $this->waite_time);
if ($data === false) {
throw new Exception('Client Waite timeout.');
}
return $data[0];
}