Skip to content

Commit 4acb3a9

Browse files
committed
Cover possible no-db related throw before touching the db
Address #6545 (comment)
1 parent f0e53dc commit 4acb3a9

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/Connection.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Doctrine\DBAL\Cache\QueryCacheProfile;
1010
use Doctrine\DBAL\Driver\API\ExceptionConverter;
1111
use Doctrine\DBAL\Driver\Connection as DriverConnection;
12+
use Doctrine\DBAL\Driver\Exception as TheDriverException;
1213
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
1314
use Doctrine\DBAL\Driver\Statement as DriverStatement;
1415
use Doctrine\DBAL\Event\TransactionBeginEventArgs;
@@ -1291,7 +1292,20 @@ public function transactional(Closure $func)
12911292
}
12921293
}
12931294

1294-
$this->commit();
1295+
$shouldRollback = true;
1296+
try {
1297+
$this->commit();
1298+
1299+
$shouldRollback = false;
1300+
} catch (TheDriverException $t) {
1301+
$shouldRollback = false;
1302+
1303+
throw $t;
1304+
} finally {
1305+
if ($shouldRollback) {
1306+
$this->rollBack();
1307+
}
1308+
}
12951309

12961310
return $res;
12971311
}

tests/ConnectionTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,28 @@ public function rollBack(): void
10251025
self::assertSame('Original exception', $e->getPrevious()->getMessage());
10261026
}
10271027
}
1028+
1029+
/**
1030+
* We are not sure if this can happen in real life scenario
1031+
*/
1032+
public function testItFailsDuringCommitBeforeTouchingDb(): void
1033+
{
1034+
$connection = new class (['memory' => true], new Driver\SQLite3\Driver()) extends Connection {
1035+
public function commit(): void
1036+
{
1037+
throw new Exception('Fail before touching the db');
1038+
}
1039+
1040+
public function rollBack(): void
1041+
{
1042+
throw new Exception('Rollback got triggered');
1043+
}
1044+
};
1045+
1046+
$this->expectExceptionMessage('Rollback got triggered');
1047+
$connection->transactional(static function (): void {
1048+
});
1049+
}
10281050
}
10291051

10301052
interface ConnectDispatchEventListener

0 commit comments

Comments
 (0)