Skip to content

Commit 0bdc850

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

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Connection.php

+8-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,13 @@ public function transactional(Closure $func)
12911292
}
12921293
}
12931294

1294-
$this->commit();
1295+
try {
1296+
$this->commit();
1297+
} finally {
1298+
if ($this->isTransactionActive()) {
1299+
$this->rollBack();
1300+
}
1301+
}
12951302

12961303
return $res;
12971304
}

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)