Skip to content

Commit 3d54447

Browse files
authored
Don't attempt to rollback when connections are lost (#7240)
| Q | A |------------- | ----------- | Type | bug #### Summary It's useless to try to perform a rollback when there's no active connection, not to mention that the use of `NoActiveTransaction` exceptions masks the real underlying issue. This attempts to fix the problem by detecting connection issues and bypassing rollbacks. Signed-off-by: Luís Cobucci <lcobucci@gmail.com>
1 parent 1cbda9f commit 3d54447

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Connection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,8 +964,11 @@ public function transactional(Closure $func): mixed
964964
$res = $func($this);
965965

966966
$successful = true;
967+
} catch (ConnectionLost $connectionLost) {
968+
// Catching here only to be able to prevent a rollback attempt
969+
throw $connectionLost;
967970
} finally {
968-
if (! $successful) {
971+
if (! isset($connectionLost) && ! $successful) {
969972
$this->rollBack();
970973
}
971974
}
@@ -981,6 +984,7 @@ public function transactional(Closure $func): mixed
981984
|| $t instanceof UniqueConstraintViolationException
982985
|| $t instanceof ForeignKeyConstraintViolationException
983986
|| $t instanceof DeadlockException
987+
|| $t instanceof ConnectionLost
984988
);
985989

986990
throw $t;

tests/Functional/TransactionTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ public function testRollbackFailure(): void
4848
});
4949
}
5050

51+
public function testTransactionalFailureDuringCallback(): void
52+
{
53+
$this->connection->transactional(
54+
function (): void {
55+
$this->expectConnectionLoss(static function (Connection $connection): void {
56+
$connection->executeQuery($connection->getDatabasePlatform()->getDummySelectSQL());
57+
});
58+
},
59+
);
60+
}
61+
62+
public function testTransactionalFailureDuringCommit(): void
63+
{
64+
$this->connection->transactional(
65+
function (): void {
66+
$this->expectConnectionLoss(static function (Connection $connection): void {
67+
});
68+
},
69+
);
70+
}
71+
5172
private function expectConnectionLoss(callable $scenario): void
5273
{
5374
$this->killCurrentSession();

0 commit comments

Comments
 (0)