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

Commit

Permalink
Сheck for active transaction in commit and rollback methods
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy-j authored Mar 18, 2021
2 parents 4818807 + 567cbe4 commit 0e163b4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
50 changes: 44 additions & 6 deletions src/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ abstract class Driver implements DriverInterface, LoggerAwareInterface
protected $pdo;

/** @var int */
protected $transactionLevel;
protected $transactionLevel = 0;

/** @var HandlerInterface */
protected $schemaHandler;
Expand All @@ -102,7 +102,6 @@ public function __construct(
CompilerInterface $queryCompiler,
BuilderInterface $queryBuilder
) {
$this->transactionLevel = 0;
$this->schemaHandler = $schemaHandler->withDriver($this);
$this->queryBuilder = $queryBuilder->withDriver($this);
$this->queryCompiler = $queryCompiler;
Expand Down Expand Up @@ -352,7 +351,7 @@ public function lastInsertID(string $sequence = null)
*/
public function beginTransaction(string $isolationLevel = null): bool
{
$this->transactionLevel++;
++$this->transactionLevel;

if ($this->transactionLevel === 1) {
if ($isolationLevel !== null) {
Expand All @@ -377,10 +376,11 @@ public function beginTransaction(string $isolationLevel = null): bool
try {
return $this->getPDO()->beginTransaction();
} catch (Throwable $e) {
$this->transactionLevel = 0;
throw $this->mapException($e, 'BEGIN TRANSACTION');
}
} else {
$this->transactionLevel--;
$this->transactionLevel = 0;
throw $e;
}
}
Expand All @@ -395,10 +395,31 @@ public function beginTransaction(string $isolationLevel = null): bool
* Commit the active database transaction.
*
* @return bool
*
* @throws StatementException
*/
public function commitTransaction(): bool
{
$this->transactionLevel--;
// Check active transaction
if (!$this->getPDO()->inTransaction()) {
if ($this->logger !== null) {
$this->logger->warning(
sprintf(
'Attempt to commit a transaction that has not yet begun. Transaction level: %d',
$this->transactionLevel
)
);
}

if ($this->transactionLevel === 0) {
return false;
}

$this->transactionLevel = 0;
return true;
}

--$this->transactionLevel;

if ($this->transactionLevel === 0) {
if ($this->logger !== null) {
Expand All @@ -421,10 +442,27 @@ public function commitTransaction(): bool
* Rollback the active database transaction.
*
* @return bool
*
* @throws StatementException
*/
public function rollbackTransaction(): bool
{
$this->transactionLevel--;
// Check active transaction
if (!$this->getPDO()->inTransaction()) {
if ($this->logger !== null) {
$this->logger->warning(
sprintf(
'Attempt to rollback a transaction that has not yet begun. Transaction level: %d',
$this->transactionLevel
)
);
}

$this->transactionLevel = 0;
return false;
}

--$this->transactionLevel;

if ($this->transactionLevel === 0) {
if ($this->logger !== null) {
Expand Down
5 changes: 3 additions & 2 deletions src/Driver/Postgres/PostgresDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function resetPrimaryKeys(): void
*/
public function beginTransaction(string $isolationLevel = null): bool
{
$this->transactionLevel++;
++$this->transactionLevel;

if ($this->transactionLevel === 1) {
if ($this->logger !== null) {
Expand All @@ -145,10 +145,11 @@ public function beginTransaction(string $isolationLevel = null): bool
try {
return $this->getPDO()->beginTransaction();
} catch (Throwable $e) {
$this->transactionLevel = 0;
throw $this->mapException($e, 'BEGIN TRANSACTION');
}
} else {
$this->transactionLevel--;
$this->transactionLevel = 0;
throw $e;
}
}
Expand Down

0 comments on commit 0e163b4

Please sign in to comment.