From 6fe699413df645591923596de2b2e25b45f18fad Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Thu, 6 Apr 2023 22:58:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Connection.php | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/Connection.php b/Connection.php index 676f996..f1d3257 100644 --- a/Connection.php +++ b/Connection.php @@ -56,6 +56,9 @@ class Connection extends Component public array $pool = ['max' => 10, 'min' => 1]; + + private int $storey = 0; + /** * @var bool * enable database cache @@ -207,7 +210,10 @@ class Connection extends Component public function beginTransaction(): static { $pdo = $this->getTransactionClient(); - $pdo->beginTransaction(); + if ($this->storey == 0) { + $pdo->beginTransaction(); + } + $this->storey++; return $this; } @@ -223,6 +229,7 @@ class Connection extends Component } $pdo = Context::get($this->cds); if ($pdo === null) { + /** @var PDO $pdo */ $pdo = Context::set($this->cds, $this->getConnection()); } return $pdo; @@ -244,12 +251,15 @@ class Connection extends Component */ public function rollback() { - $pdo = $this->getTransactionClient(); - if ($pdo->inTransaction()) { - $pdo->rollback(); + $this->storey--; + if ($this->storey == 0) { + $pdo = $this->getTransactionClient(); + if ($pdo->inTransaction()) { + $pdo->rollback(); + } + $this->connections->push($this->cds, $pdo); + Context::remove($this->cds); } - $this->connections->push($this->cds, $pdo); - Context::remove($this->cds); } /** @@ -258,12 +268,15 @@ class Connection extends Component */ public function commit() { - $pdo = $this->getTransactionClient(); - if ($pdo->inTransaction()) { - $pdo->commit(); + $this->storey--; + if ($this->storey == 0) { + $pdo = $this->getTransactionClient(); + if ($pdo->inTransaction()) { + $pdo->commit(); + } + $this->connections->push($this->cds, $pdo); + Context::remove($this->cds); } - $this->connections->push($this->cds, $pdo); - Context::remove($this->cds); }