Skip to content

Commit 518a2bf

Browse files
committed
do not start a transaction if there is already one
1 parent 2f76624 commit 518a2bf

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/Storage/PDOConnector.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class PDOConnector implements IStorageConnector {
3939
private $pdo = null;
4040
/** @var \PDOStatement $statement */
4141
private $statement = null;
42+
/** @var bool $transactionExists */
43+
private $transactionExists = false;
4244

4345
/**
4446
* sets the credentials used to connect against the database
@@ -54,23 +56,38 @@ public function setCredentials(array $credentials) {
5456
* @return bool
5557
*/
5658
public function startTransaction(): bool {
57-
return $this->pdo->beginTransaction();
59+
if ($this->transactionExists) {
60+
return false;
61+
}
62+
$started = $this->pdo->beginTransaction();
63+
$this->transactionExists = $started;
64+
return $started;
5865
}
5966

6067
/**
6168
* commits a started database transaction
6269
* @return bool
6370
*/
6471
public function commit(): bool {
65-
return $this->pdo->commit();
72+
if ($this->transactionExists) {
73+
$commited = $this->pdo->commit();
74+
$this->transactionExists = !$commited;
75+
return $commited;
76+
}
77+
return false;
6678
}
6779

6880
/**
6981
* rolls a started transaction back
7082
* @return bool
7183
*/
7284
public function rollback(): bool {
73-
return $this->pdo->rollBack();
85+
if ($this->transactionExists) {
86+
$rolledBack = $this->pdo->rollBack();
87+
$this->transactionExists = !$rolledBack;
88+
return $rolledBack;
89+
}
90+
return false;
7491
}
7592

7693
/**

0 commit comments

Comments
 (0)