This commit is contained in:
2023-03-31 23:36:07 +08:00
parent 656753cf60
commit c165f5f205
2 changed files with 19 additions and 37 deletions
+6 -6
View File
@@ -142,17 +142,17 @@ class Command extends Component
$result = (int)$pdo->lastInsertId(); $result = (int)$pdo->lastInsertId();
$prepare->closeCursor(); $prepare->closeCursor();
$this->db->release($pdo, true); $this->db->release($pdo);
return $result == 0 ? true : $result; return $result == 0 ? true : $result;
} catch (\PDOException|\Throwable $throwable) { } catch (\PDOException|\Throwable $throwable) {
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
$this->db->restore(true); $this->db->restore();
return $this->_execute(); return $this->_execute();
} }
$this->db->release($pdo,true); $this->db->release($pdo);
return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql'); return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql');
} }
@@ -174,16 +174,16 @@ class Command extends Component
$statement->bindValue($key, $param); $statement->bindValue($key, $param);
} }
$data = $statement->{$type}(\PDO::FETCH_ASSOC); $data = $statement->{$type}(\PDO::FETCH_ASSOC);
$this->db->release($pdo, false); $this->db->release($pdo);
return $data; return $data;
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) { if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
$this->db->restore(false); $this->db->restore();
return $this->search($type); return $this->search($type);
} }
$this->db->release($pdo, false); $this->db->release($pdo);
return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql'); return $this->logger->addError($this->sql . '. error: ' . $throwable->getMessage(), 'mysql');
} }
+13 -31
View File
@@ -117,15 +117,9 @@ class Connection extends Component
*/ */
public function connectPoolInstance() public function connectPoolInstance()
{ {
if (!empty($this->slaveConfig) && isset($this->slaveConfig['cds'])) { $pool = $this->slaveConfig['pool'] ?? ['max' => 10, 'min' => 1];
$pool = $this->pool ?? ['max' => 10, 'min' => 1];
$this->connection->initConnections($this->cds, $pool['max']);
$this->connection->initConnections($this->slaveConfig['cds'] . 'slave', $pool['max']);
} else {
$pool = $this->slaveConfig['pool'] ?? ['max' => 10, 'min' => 1];
$this->connection->initConnections($this->cds . 'master', $pool['max']);
}
} }
@@ -162,7 +156,7 @@ class Connection extends Component
'read_timeout' => $this->read_timeout, 'read_timeout' => $this->read_timeout,
'dbname' => $this->database, 'dbname' => $this->database,
'pool' => $this->pool 'pool' => $this->pool
], true); ]);
} }
/** /**
@@ -171,16 +165,7 @@ class Connection extends Component
*/ */
public function getSlaveClient(): PDO public function getSlaveClient(): PDO
{ {
return $this->connection->get([ return $this->getMasterClient();
'cds' => $this->slaveConfig['cds'] ?? $this->cds,
'username' => $this->slaveConfig['username'] ?? $this->username,
'password' => $this->slaveConfig['password'] ?? $this->password,
'attributes' => $this->slaveConfig['attributes'] ?? $this->attributes,
'connect_timeout' => $this->connect_timeout,
'read_timeout' => $this->read_timeout,
'dbname' => $this->slaveConfig['database'] ?? $this->database,
'pool' => $this->pool
], false);
} }
/** /**
@@ -223,7 +208,7 @@ class Connection extends Component
if ($pdo->inTransaction()) { if ($pdo->inTransaction()) {
$pdo->rollback(); $pdo->rollback();
} }
$this->release($pdo,true); $this->release($pdo);
} }
/** /**
@@ -236,7 +221,7 @@ class Connection extends Component
if ($pdo->inTransaction()) { if ($pdo->inTransaction()) {
$pdo->commit(); $pdo->commit();
} }
$this->release($pdo,true); $this->release($pdo);
} }
@@ -253,9 +238,9 @@ class Connection extends Component
} }
public function restore($isMaster) public function restore()
{ {
Context::remove($this->alias($isMaster)); Context::remove($this->cds);
} }
@@ -264,16 +249,13 @@ class Connection extends Component
* 回收链接 * 回收链接
* @throws * @throws
*/ */
public function release(PDO $PDO, bool $isMaster) public function release(PDO $PDO)
{ {
$name = $this->alias($isMaster); if ($PDO->inTransaction()) {
if ($isMaster && $PDO->inTransaction()) {
return; return;
} }
$this->connection->addItem($name, $PDO); $this->connection->addItem($this->cds, $PDO);
if ($isMaster) { Context::remove($this->cds);
Context::remove($name);
}
} }