modify plugin name

This commit is contained in:
2022-06-08 16:13:16 +08:00
parent eb36a30b26
commit 62591edf44
2 changed files with 42 additions and 31 deletions
+3 -3
View File
@@ -137,13 +137,13 @@ class Command extends Component
*/ */
private function _execute(): bool|int private function _execute(): bool|int
{ {
$pdo = $this->db->masterInstance(); $pdo = $this->db->getPdo();
try { try {
$result = $pdo->execute($this->sql, $this->params); $result = $pdo->execute($this->sql, $this->params);
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$result = $this->logger->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql'); $result = $this->logger->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
} finally { } finally {
$this->db->release($pdo, true); $this->db->release($pdo);
return $result; return $result;
} }
} }
@@ -156,7 +156,7 @@ class Command extends Component
*/ */
private function search(string $type): array|int|bool|null private function search(string $type): array|int|bool|null
{ {
$pdo = $this->db->slaveInstance(); $pdo = $this->db->getSlaveClient();
try { try {
$data = $pdo->{$type}($this->sql, $this->params); $data = $pdo->{$type}($this->sql, $this->params);
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
+39 -28
View File
@@ -20,7 +20,6 @@ use Exception;
use Kiri; use Kiri;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Context;
use Kiri\Events\EventProvider; use Kiri\Events\EventProvider;
use Kiri\Exception\NotFindClassException; use Kiri\Exception\NotFindClassException;
use Kiri\Server\Events\OnWorkerExit; use Kiri\Server\Events\OnWorkerExit;
@@ -56,6 +55,9 @@ class Connection extends Component
public bool $enableCache = false; public bool $enableCache = false;
private ?PDO $_pdo = null;
/** /**
* @var string * @var string
*/ */
@@ -104,7 +106,7 @@ class Connection extends Component
*/ */
public function getConnect($isSearch): PDO public function getConnect($isSearch): PDO
{ {
return !$isSearch ? $this->masterInstance() : $this->slaveInstance(); return !$isSearch ? $this->getMasterClient() : $this->getSlaveClient();
} }
@@ -145,7 +147,7 @@ class Connection extends Component
* @return PDO * @return PDO
* @throws Exception * @throws Exception
*/ */
public function masterInstance(): PDO public function getMasterClient(): PDO
{ {
return $this->connections()->get([ return $this->connections()->get([
'cds' => $this->cds, 'cds' => $this->cds,
@@ -163,10 +165,10 @@ class Connection extends Component
* @return PDO * @return PDO
* @throws Exception * @throws Exception
*/ */
public function slaveInstance(): PDO public function getSlaveClient(): PDO
{ {
if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) { if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) {
return $this->masterInstance(); return $this->getMasterClient();
} }
return $this->connections()->get($this->slaveConfig); return $this->connections()->get($this->slaveConfig);
} }
@@ -188,19 +190,34 @@ class Connection extends Component
*/ */
public function beginTransaction(): static public function beginTransaction(): static
{ {
$pdo = $this->masterInstance(); $pdo = $this->getMasterClient();
$pdo->beginTransaction(); $pdo->beginTransaction();
return $this; return $this;
} }
/**
* @return PDO
* @throws Exception
*/
public function getPdo(): PDO
{
if (!$this->_pdo) {
$this->_pdo = $this->getMasterClient();
}
return $this->_pdo;
}
/** /**
* @return $this|bool * @return $this|bool
* @throws Exception * @throws Exception
*/ */
public function inTransaction(): bool|static public function inTransaction(): bool|static
{ {
$pdo = $this->masterInstance(); if (!$this->_pdo) {
return $pdo->inTransaction(); $this->_pdo = $this->getMasterClient();
}
return $this->_pdo->inTransaction();
} }
/** /**
@@ -209,11 +226,11 @@ class Connection extends Component
*/ */
public function rollback() public function rollback()
{ {
$pdo = $this->masterInstance(); if ($this->_pdo->inTransaction()) {
if ($pdo->inTransaction()) { $this->_pdo->rollback();
$pdo->rollback();
} }
$this->release($pdo, $this->cds); $this->release($this->_pdo);
$this->_pdo = null;
} }
/** /**
@@ -222,11 +239,11 @@ class Connection extends Component
*/ */
public function commit() public function commit()
{ {
$pdo = $this->masterInstance(); if ($this->_pdo->inTransaction()) {
if ($pdo->inTransaction()) { $this->_pdo->commit();
$pdo->commit();
} }
$this->release($pdo, $this->cds); $this->release($this->_pdo);
$this->_pdo = null;
} }
@@ -248,21 +265,15 @@ class Connection extends Component
* 回收链接 * 回收链接
* @throws * @throws
*/ */
public function release(PDO $pdo, $isMaster) public function release(PDO $pdo)
{ {
$connections = $this->connections(); $connections = $this->connections();
if ($pdo->inTransaction()) { if (!$pdo->inTransaction()) {
return; $cds = $this->cds;
} if (isset($this->slaveConfig['cds'])) {
Context::remove($this->cds); $cds = $this->slaveConfig['cds'];
Context::remove($this->slaveConfig['cds']);
if (!$isMaster) {
if (!isset($this->slaveConfig['cds'])) {
$this->slaveConfig['cds'] = $this->cds;
} }
$connections->addItem($this->slaveConfig['cds'], $pdo); $connections->addItem($cds, $pdo);
} else {
$connections->addItem($this->cds, $pdo);
} }
} }