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
+27 -12
View File
@@ -206,21 +206,36 @@ class Connection extends Component
*/
public function beginTransaction(): static
{
$pdo = Context::get($this->cds);
if ($pdo === null) {
$pdo = $this->getConnection();
}
$pdo = $this->getTransactionClient();
$pdo->beginTransaction();
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
* @throws Exception
*/
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()
{
$pdo = Context::get($this->cds);
$pdo = $this->getTransactionClient();
if ($pdo->inTransaction()) {
$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()
{
$pdo = Context::get($this->cds);
$pdo = $this->getTransactionClient();
if ($pdo->inTransaction()) {
$pdo->commit();
}
$this->release($pdo);
$this->connections->push($this->cds, $pdo);
Context::remove($this->cds);
}
@@ -270,9 +287,7 @@ class Connection extends Component
*/
public function release(PDO $PDO)
{
if ($PDO->inTransaction() === false) {
$this->connections->push($this->cds, $PDO);
}
$this->connections->push($this->cds, $PDO);
}