Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

Commit 0e163b4

Browse files
authored
Сheck for active transaction in commit and rollback methods
2 parents 4818807 + 567cbe4 commit 0e163b4

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/Driver/Driver.php

+44-6
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ abstract class Driver implements DriverInterface, LoggerAwareInterface
7676
protected $pdo;
7777

7878
/** @var int */
79-
protected $transactionLevel;
79+
protected $transactionLevel = 0;
8080

8181
/** @var HandlerInterface */
8282
protected $schemaHandler;
@@ -102,7 +102,6 @@ public function __construct(
102102
CompilerInterface $queryCompiler,
103103
BuilderInterface $queryBuilder
104104
) {
105-
$this->transactionLevel = 0;
106105
$this->schemaHandler = $schemaHandler->withDriver($this);
107106
$this->queryBuilder = $queryBuilder->withDriver($this);
108107
$this->queryCompiler = $queryCompiler;
@@ -352,7 +351,7 @@ public function lastInsertID(string $sequence = null)
352351
*/
353352
public function beginTransaction(string $isolationLevel = null): bool
354353
{
355-
$this->transactionLevel++;
354+
++$this->transactionLevel;
356355

357356
if ($this->transactionLevel === 1) {
358357
if ($isolationLevel !== null) {
@@ -377,10 +376,11 @@ public function beginTransaction(string $isolationLevel = null): bool
377376
try {
378377
return $this->getPDO()->beginTransaction();
379378
} catch (Throwable $e) {
379+
$this->transactionLevel = 0;
380380
throw $this->mapException($e, 'BEGIN TRANSACTION');
381381
}
382382
} else {
383-
$this->transactionLevel--;
383+
$this->transactionLevel = 0;
384384
throw $e;
385385
}
386386
}
@@ -395,10 +395,31 @@ public function beginTransaction(string $isolationLevel = null): bool
395395
* Commit the active database transaction.
396396
*
397397
* @return bool
398+
*
399+
* @throws StatementException
398400
*/
399401
public function commitTransaction(): bool
400402
{
401-
$this->transactionLevel--;
403+
// Check active transaction
404+
if (!$this->getPDO()->inTransaction()) {
405+
if ($this->logger !== null) {
406+
$this->logger->warning(
407+
sprintf(
408+
'Attempt to commit a transaction that has not yet begun. Transaction level: %d',
409+
$this->transactionLevel
410+
)
411+
);
412+
}
413+
414+
if ($this->transactionLevel === 0) {
415+
return false;
416+
}
417+
418+
$this->transactionLevel = 0;
419+
return true;
420+
}
421+
422+
--$this->transactionLevel;
402423

403424
if ($this->transactionLevel === 0) {
404425
if ($this->logger !== null) {
@@ -421,10 +442,27 @@ public function commitTransaction(): bool
421442
* Rollback the active database transaction.
422443
*
423444
* @return bool
445+
*
446+
* @throws StatementException
424447
*/
425448
public function rollbackTransaction(): bool
426449
{
427-
$this->transactionLevel--;
450+
// Check active transaction
451+
if (!$this->getPDO()->inTransaction()) {
452+
if ($this->logger !== null) {
453+
$this->logger->warning(
454+
sprintf(
455+
'Attempt to rollback a transaction that has not yet begun. Transaction level: %d',
456+
$this->transactionLevel
457+
)
458+
);
459+
}
460+
461+
$this->transactionLevel = 0;
462+
return false;
463+
}
464+
465+
--$this->transactionLevel;
428466

429467
if ($this->transactionLevel === 0) {
430468
if ($this->logger !== null) {

src/Driver/Postgres/PostgresDriver.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function resetPrimaryKeys(): void
119119
*/
120120
public function beginTransaction(string $isolationLevel = null): bool
121121
{
122-
$this->transactionLevel++;
122+
++$this->transactionLevel;
123123

124124
if ($this->transactionLevel === 1) {
125125
if ($this->logger !== null) {
@@ -145,10 +145,11 @@ public function beginTransaction(string $isolationLevel = null): bool
145145
try {
146146
return $this->getPDO()->beginTransaction();
147147
} catch (Throwable $e) {
148+
$this->transactionLevel = 0;
148149
throw $this->mapException($e, 'BEGIN TRANSACTION');
149150
}
150151
} else {
151-
$this->transactionLevel--;
152+
$this->transactionLevel = 0;
152153
throw $e;
153154
}
154155
}

0 commit comments

Comments
 (0)