This commit is contained in:
2023-04-06 22:54:50 +08:00
parent 4e978cf919
commit 5a96ca96cc
2 changed files with 28 additions and 15 deletions
+1 -3
View File
@@ -170,7 +170,7 @@ class Command extends Component
private function _execute(): bool|int private function _execute(): bool|int
{ {
try { try {
$client = $this->connection->getConnection(); $client = $this->connection->getTransactionClient();
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]);
} }
@@ -185,8 +185,6 @@ class Command extends Component
return $this->_execute(); return $this->_execute();
} }
return $this->error($throwable); return $this->error($throwable);
} finally {
$this->connection->release($client ?? null);
} }
} }
+26 -11
View File
@@ -206,21 +206,36 @@ class Connection extends Component
*/ */
public function beginTransaction(): static public function beginTransaction(): static
{ {
$pdo = Context::get($this->cds); $pdo = $this->getTransactionClient();
if ($pdo === null) {
$pdo = $this->getConnection();
}
$pdo->beginTransaction(); $pdo->beginTransaction();
return $this; return $this;
} }
/**
* @return PDO
* @throws Exception
*/
public function getTransactionClient(): PDO
{
if (!Db::inTransactionsActive()) {
return $this->getConnection();
}
$pdo = Context::get($this->cds);
if ($pdo === null) {
$pdo = Context::set($this->cds, $this->getConnection());
}
return $pdo;
}
/** /**
* @return $this|bool * @return $this|bool
* @throws Exception * @throws Exception
*/ */
public function inTransaction(): bool|static public function inTransaction(): bool|static
{ {
return Context::get($this->cds)->inTransaction(); $pdo = $this->getTransactionClient();
return $pdo->inTransaction();
} }
/** /**
@@ -229,11 +244,12 @@ class Connection extends Component
*/ */
public function rollback() public function rollback()
{ {
$pdo = Context::get($this->cds); $pdo = $this->getTransactionClient();
if ($pdo->inTransaction()) { if ($pdo->inTransaction()) {
$pdo->rollback(); $pdo->rollback();
} }
$this->release($pdo); $this->connections->push($this->cds, $pdo);
Context::remove($this->cds);
} }
/** /**
@@ -242,11 +258,12 @@ class Connection extends Component
*/ */
public function commit() public function commit()
{ {
$pdo = Context::get($this->cds); $pdo = $this->getTransactionClient();
if ($pdo->inTransaction()) { if ($pdo->inTransaction()) {
$pdo->commit(); $pdo->commit();
} }
$this->release($pdo); $this->connections->push($this->cds, $pdo);
Context::remove($this->cds);
} }
@@ -270,10 +287,8 @@ class Connection extends Component
*/ */
public function release(PDO $PDO) public function release(PDO $PDO)
{ {
if ($PDO->inTransaction() === false) {
$this->connections->push($this->cds, $PDO); $this->connections->push($this->cds, $PDO);
} }
}
/** /**