Skip to content

Commit

Permalink
Merge branch '3.9.x' into 4.2.x
Browse files Browse the repository at this point in the history
* 3.9.x:
  Run risky code in finally block (#6543)
  • Loading branch information
derrabus committed Oct 11, 2024
2 parents dadd353 + c204fe1 commit d21506a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -926,15 +926,20 @@ public function lastInsertId(): int|string
public function transactional(Closure $func): mixed
{
$this->beginTransaction();

$successful = false;

try {
$res = $func($this);
$this->commit();

return $res;
} catch (Throwable $e) {
$this->rollBack();
$successful = true;

throw $e;
return $res;
} finally {
if (! $successful) {
$this->rollBack();
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,4 +623,25 @@ public function testDefaultSchemaManagerFactory(): void
$connection = DriverManager::getConnection(['driver' => 'sqlite3', 'memory' => true]);
self::assertInstanceOf(SQLiteSchemaManager::class, $connection->createSchemaManager());
}

public function testItPreservesTheOriginalExceptionOnRollbackFailure(): void
{
$connection = new class (['memory' => true], new Driver\SQLite3\Driver()) extends Connection {
public function rollBack(): void
{
throw new Exception('Rollback exception');

Check failure on line 632 in tests/ConnectionTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Cannot instantiate interface Doctrine\DBAL\Exception.

Check failure on line 632 in tests/ConnectionTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.3)

InterfaceInstantiation

tests/ConnectionTest.php:632:27: InterfaceInstantiation: Interface Doctrine\DBAL\Exception cannot be instantiated (see https://psalm.dev/158)
}
};

try {
$connection->transactional(static function (): void {
throw new Exception('Original exception');

Check failure on line 638 in tests/ConnectionTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Cannot instantiate interface Doctrine\DBAL\Exception.

Check failure on line 638 in tests/ConnectionTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.3)

InterfaceInstantiation

tests/ConnectionTest.php:638:27: InterfaceInstantiation: Interface Doctrine\DBAL\Exception cannot be instantiated (see https://psalm.dev/158)
});
self::fail('Exception expected');

Check failure on line 640 in tests/ConnectionTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.3)

Unreachable statement - code above always terminates.
} catch (Exception $e) {
self::assertSame('Rollback exception', $e->getMessage());
self::assertNotNull($e->getPrevious());
self::assertSame('Original exception', $e->getPrevious()->getMessage());
}
}
}

0 comments on commit d21506a

Please sign in to comment.