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

Commit

Permalink
- fixes bug with invalid transaction level when transaction can't be …
Browse files Browse the repository at this point in the history
…started

- set isolation level after beginning the transaction for Postgres
  • Loading branch information
wolfy-j committed Aug 28, 2020
1 parent 771cdb1 commit 349decf
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 14 deletions.
15 changes: 10 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
======================

v2.7.16 (28.08.2020)
-----
- fixes bug with invalid transaction level when transaction can't be started
- set isolation level after beginning the transaction for Postgres

v2.7.15 (17.06.2020)
-----
- handle Docker specific connection exceptions (broken pipe)
Expand Down Expand Up @@ -82,16 +87,16 @@ v2.7.2 (18.01.2020)

2.6.8 (24.12.2019)
-----
- [bufgix] proper abstract type detection for primary UUID columns for SQLite driver
- [bufgix] proper abstract type detection for primary UUID columns for SQLite driver

2.6.7 (23.12.2019)
-----
- [bufgix] proper exception type for syntax errors in MariaDB (previously was ConnectionException)
- [bufgix] proper exception type for syntax errors in MariaDB (previously was ConnectionException)

2.6.6 (11.12.2019)
-----
- allow drivers to handle low level error exceptions
- qualify "Connection reset by peer" as connection exception
- allow drivers to handle low level error exceptions
- qualify "Connection reset by peer" as connection exception
- fixed interpolation of named parameters

2.6.5 (11.12.2019)
Expand Down Expand Up @@ -203,7 +208,7 @@ v2.7.2 (18.01.2020)
- new interface for driver, database, table, compiler and handler
- immutable quoter
- more tests
- custom exceptions for connection and constrain exceptions
- custom exceptions for connection and constrain exceptions

1.0.1 (15.06.2018)
-----
Expand Down
15 changes: 9 additions & 6 deletions src/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,22 @@ abstract class Driver implements DriverInterface, LoggerAwareInterface
];

/** @var PDO|null */
private $pdo;
protected $pdo;

/** @var int */
private $transactionLevel;
protected $transactionLevel;

/** @var HandlerInterface */
private $schemaHandler;
protected $schemaHandler;

/** @var CompilerInterface */
private $queryCompiler;
protected $queryCompiler;

/** @var BuilderInterface */
private $queryBuilder;
protected $queryBuilder;

/** @var PDOStatement[] */
private $queryCache = [];
protected $queryCache = [];

/**
* @param array $options
Expand Down Expand Up @@ -379,6 +379,9 @@ public function beginTransaction(string $isolationLevel = null): bool
} catch (Throwable $e) {
throw $this->mapException($e, 'BEGIN TRANSACTION');
}
} else {
$this->transactionLevel--;
throw $e;
}
}
}
Expand Down
58 changes: 55 additions & 3 deletions src/Driver/Postgres/PostgresDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public function getPrimaryKey(string $prefix, string $table): ?string
}

$this->primaryKeys[$name] = $this->getSchemaHandler()
->getSchema($table, $prefix)
->getPrimaryKeys();
->getSchema($table, $prefix)
->getPrimaryKeys();

if (count($this->primaryKeys[$name]) === 1) {
//We do support only single primary key
Expand All @@ -106,6 +106,58 @@ public function resetPrimaryKeys(): void
$this->primaryKeys = [];
}

/**
* Start SQL transaction with specified isolation level (not all DBMS support it). Nested
* transactions are processed using savepoints.
*
* @link http://en.wikipedia.org/wiki/Database_transaction
* @link http://en.wikipedia.org/wiki/Isolation_(database_systems)
*
* @param string $isolationLevel
* @return bool
*/
public function beginTransaction(string $isolationLevel = null): bool
{
$this->transactionLevel++;

if ($this->transactionLevel === 1) {
if ($this->logger !== null) {
$this->logger->info('Begin transaction');
}

try {
$ok = $this->getPDO()->beginTransaction();
if ($isolationLevel !== null) {
$this->setIsolationLevel($isolationLevel);
}

return $ok;
} catch (Throwable $e) {
$e = $this->mapException($e, 'BEGIN TRANSACTION');

if (
$e instanceof StatementException\ConnectionException
&& $this->options['reconnect']
) {
$this->disconnect();

try {
return $this->getPDO()->beginTransaction();
} catch (Throwable $e) {
throw $this->mapException($e, 'BEGIN TRANSACTION');
}
} else {
$this->transactionLevel--;
throw $e;
}
}
}

$this->createSavepoint($this->transactionLevel);

return true;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -135,7 +187,7 @@ protected function mapException(Throwable $exception, string $query): StatementE
return new StatementException\ConnectionException($exception, $query);
}

if ((int)$exception->getCode() >= 23000 && (int)$exception->getCode() < 24000) {
if ((int) $exception->getCode() >= 23000 && (int) $exception->getCode() < 24000) {
return new StatementException\ConstrainException($exception, $query);
}

Expand Down

0 comments on commit 349decf

Please sign in to comment.