This commit is contained in:
2023-04-06 22:58:43 +08:00
parent 84d1701ff7
commit 6fe699413d
+13
View File
@@ -56,6 +56,9 @@ class Connection extends Component
public array $pool = ['max' => 10, 'min' => 1]; public array $pool = ['max' => 10, 'min' => 1];
private int $storey = 0;
/** /**
* @var bool * @var bool
* enable database cache * enable database cache
@@ -207,7 +210,10 @@ class Connection extends Component
public function beginTransaction(): static public function beginTransaction(): static
{ {
$pdo = $this->getTransactionClient(); $pdo = $this->getTransactionClient();
if ($this->storey == 0) {
$pdo->beginTransaction(); $pdo->beginTransaction();
}
$this->storey++;
return $this; return $this;
} }
@@ -223,6 +229,7 @@ class Connection extends Component
} }
$pdo = Context::get($this->cds); $pdo = Context::get($this->cds);
if ($pdo === null) { if ($pdo === null) {
/** @var PDO $pdo */
$pdo = Context::set($this->cds, $this->getConnection()); $pdo = Context::set($this->cds, $this->getConnection());
} }
return $pdo; return $pdo;
@@ -244,6 +251,8 @@ class Connection extends Component
*/ */
public function rollback() public function rollback()
{ {
$this->storey--;
if ($this->storey == 0) {
$pdo = $this->getTransactionClient(); $pdo = $this->getTransactionClient();
if ($pdo->inTransaction()) { if ($pdo->inTransaction()) {
$pdo->rollback(); $pdo->rollback();
@@ -251,6 +260,7 @@ class Connection extends Component
$this->connections->push($this->cds, $pdo); $this->connections->push($this->cds, $pdo);
Context::remove($this->cds); Context::remove($this->cds);
} }
}
/** /**
* @throws Exception * @throws Exception
@@ -258,6 +268,8 @@ class Connection extends Component
*/ */
public function commit() public function commit()
{ {
$this->storey--;
if ($this->storey == 0) {
$pdo = $this->getTransactionClient(); $pdo = $this->getTransactionClient();
if ($pdo->inTransaction()) { if ($pdo->inTransaction()) {
$pdo->commit(); $pdo->commit();
@@ -265,6 +277,7 @@ class Connection extends Component
$this->connections->push($this->cds, $pdo); $this->connections->push($this->cds, $pdo);
Context::remove($this->cds); Context::remove($this->cds);
} }
}
/** /**