Skip to content

Convert driver exceptions when starting transactions #6812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,11 @@
++$this->transactionNestingLevel;

if ($this->transactionNestingLevel === 1) {
$connection->beginTransaction();
try {
$connection->beginTransaction();
} catch (Driver\Exception $e) {

Check warning on line 1032 in src/Connection.php

View check run for this annotation

Codecov / codecov/patch

src/Connection.php#L1032

Added line #L1032 was not covered by tests
throw $this->convertException($e);
}
} else {
$this->createSavepoint($this->_getNestedTransactionSavePointName());
}
Expand Down
5 changes: 5 additions & 0 deletions src/Driver/API/PostgreSQL/ExceptionConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface;
use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Exception\ConnectionException;
use Doctrine\DBAL\Exception\ConnectionLost;
use Doctrine\DBAL\Exception\DatabaseDoesNotExist;
use Doctrine\DBAL\Exception\DeadlockException;
use Doctrine\DBAL\Exception\DriverException;
Expand Down Expand Up @@ -77,6 +78,10 @@
return new ConnectionException($exception, $query);
}

if (str_contains($exception->getMessage(), 'terminating connection')) {
return new ConnectionLost($exception, $query);
}

Check warning on line 84 in src/Driver/API/PostgreSQL/ExceptionConverter.php

View check run for this annotation

Codecov / codecov/patch

src/Driver/API/PostgreSQL/ExceptionConverter.php#L81-L84

Added lines #L81 - L84 were not covered by tests
return new DriverException($exception, $query);
}
}
4 changes: 3 additions & 1 deletion src/Driver/Mysqli/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public function lastInsertId(): int|string

public function beginTransaction(): void
{
$this->connection->begin_transaction();
if (! $this->connection->begin_transaction()) {
throw ConnectionError::new($this->connection);
}
}

public function commit(): void
Expand Down
50 changes: 39 additions & 11 deletions tests/Functional/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,48 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception\ConnectionLost;
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;
use Doctrine\DBAL\Types\Types;

use function func_get_args;
use function restore_error_handler;
use function set_error_handler;
use function sleep;

use const E_WARNING;

class TransactionTest extends FunctionalTestCase
{
public function testBeginTransactionFailure(): void
{
$this->expectConnectionLoss(static function (Connection $connection): void {
$connection->beginTransaction();
});
}

public function testCommitFailure(): void
{
$this->connection->beginTransaction();

$this->expectConnectionLoss(static function (Connection $connection): void {
$connection->commit();
});
}

public function testRollbackFailure(): void
{
$this->connection->beginTransaction();

$this->expectConnectionLoss(static function (Connection $connection): void {
$connection->rollBack();
});
}

private function expectConnectionLoss(callable $scenario): void
{
if (! $this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform) {
self::markTestSkipped('Restricted to MySQL.');
}

$this->connection->executeStatement('SET SESSION wait_timeout=1');
$this->connection->beginTransaction();

// during the sleep MySQL will close the connection
sleep(2);

$this->killCurrentSession();
$this->expectException(ConnectionLost::class);

// prevent the PHPUnit error handler from handling the "MySQL server has gone away" warning
Expand All @@ -65,6 +68,31 @@ private function expectConnectionLoss(callable $scenario): void
}
}

private function killCurrentSession(): void
{
$this->markConnectionNotReusable();

$databasePlatform = $this->connection->getDatabasePlatform();

[$currentProcessQuery, $killProcessStatement] = match (true) {
$databasePlatform instanceof AbstractMySqlPlatform => [
'SELECT CONNECTION_ID()',
'KILL ?',
],
$databasePlatform instanceof PostgreSQLPlatform => [
'SELECT pg_backend_pid()',
'SELECT pg_terminate_backend(?)',
],
default => self::markTestSkipped('Unsupported test platform.'),
};

$privilegedConnection = TestUtil::getPrivilegedConnection();
$privilegedConnection->executeStatement(
$killProcessStatement,
[$this->connection->executeQuery($currentProcessQuery)->fetchOne()],
);
}

public function testNestedTransactionWalkthrough(): void
{
if (! $this->connection->getDatabasePlatform()->supportsSavepoints()) {
Expand Down
Loading