This commit is contained in:
2023-08-24 11:25:42 +08:00
parent 2e01419960
commit a85fc58630
2 changed files with 18 additions and 18 deletions
+8 -8
View File
@@ -77,8 +77,8 @@ class Command extends Component
*/ */
public function all(): bool|array public function all(): bool|array
{ {
$client = $this->connection->getConnection();
try { try {
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) { if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]); throw new Exception($client->errorInfo()[1]);
} }
@@ -91,7 +91,7 @@ class Command extends Component
} }
return $result; return $result;
} finally { } finally {
$this->connection->release($client ?? null); $this->connection->release($client);
} }
} }
@@ -101,8 +101,8 @@ class Command extends Component
*/ */
public function one(): null|bool|array public function one(): null|bool|array
{ {
$client = $this->connection->getConnection();
try { try {
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) { if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]); throw new Exception($client->errorInfo()[1]);
} }
@@ -115,7 +115,7 @@ class Command extends Component
} }
return $result; return $result;
} finally { } finally {
$this->connection->release($client ?? null); $this->connection->release($client);
} }
} }
@@ -125,8 +125,8 @@ class Command extends Component
*/ */
public function fetchColumn(): mixed public function fetchColumn(): mixed
{ {
$client = $this->connection->getConnection();
try { try {
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) { if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]); throw new Exception($client->errorInfo()[1]);
} }
@@ -139,7 +139,7 @@ class Command extends Component
} }
return $result; return $result;
} finally { } finally {
$this->connection->release($client ?? null); $this->connection->release($client);
} }
} }
@@ -161,8 +161,8 @@ class Command extends Component
*/ */
private function _execute(): bool|int private function _execute(): bool|int
{ {
$client = $this->connection->getConnection();
try { try {
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) { if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]); throw new Exception($client->errorInfo()[1]);
} }
@@ -182,7 +182,7 @@ class Command extends Component
} }
return $result; return $result;
} finally { } finally {
$this->connection->release($client ?? null); $this->connection->release($client);
} }
} }
+10 -10
View File
@@ -48,7 +48,7 @@ class Connection extends Component
public string $database = ''; public string $database = '';
public int $connect_timeout = 30; public int $timeout = 30;
public int $waite_time = 3; public int $waite_time = 3;
@@ -160,10 +160,11 @@ class Connection extends Component
} }
[$client, $time] = $data; [$client, $time] = $data;
if ((time() - $time) < $this->idle_time && $this->canUse($client)) { if ((time() - $time) < $this->timeout && $this->canUse($client)) {
return $client; return $client;
} }
$this->logger->alert('PDO连接已失效, 空闲超时或已不可用,重新获取.');
$this->pool()->abandon($this->cds); $this->pool()->abandon($this->cds);
Waite::sleep(10); Waite::sleep(10);
@@ -199,11 +200,10 @@ class Connection extends Component
return false; return false;
} }
try { try {
$steam = $client->query('select 1'); if (($steam = $client->query('select 1')) === false) {
if ($steam instanceof PDOStatement) { return false;
return true;
} }
return false; return $steam->fetch(PDO::FETCH_ASSOC);
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
return false; return false;
} }
@@ -305,9 +305,9 @@ class Connection extends Component
* 回收链接 * 回收链接
* @throws * @throws
*/ */
public function release(?PDO $PDO): void public function release(PDO $PDO): void
{ {
if ($PDO === null || $PDO->inTransaction()) { if ($PDO->inTransaction()) {
return; return;
} }
$this->pool()->push($this->cds, [$PDO, time()]); $this->pool()->push($this->cds, [$PDO, time()]);
@@ -345,7 +345,7 @@ class Connection extends Component
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_STRINGIFY_FETCHES => false, PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => true, PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_TIMEOUT => $this->connect_timeout, PDO::ATTR_TIMEOUT => $this->timeout,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset
]; ];
$link = new PDO('mysql:dbname=' . $this->database . ';host=' . $this->cds, $this->username, $this->password, $options); $link = new PDO('mysql:dbname=' . $this->database . ';host=' . $this->cds, $this->username, $this->password, $options);