Skip to content

Commit

Permalink
Cover possible no-db related throw before touching the db
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Oct 12, 2024
1 parent f0e53dc commit cf80105
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Driver\API\ExceptionConverter;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\Exception as TheDriverException;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Event\TransactionBeginEventArgs;
Expand Down Expand Up @@ -1291,7 +1292,18 @@ public function transactional(Closure $func)
}
}

$this->commit();
$shouldRollback = true;
try {
$this->commit();
} catch (TheDriverException $t) {
$shouldRollback = false;

throw $t;
} finally {
if ($shouldRollback) {
$this->rollBack();
}
}

return $res;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,28 @@ public function rollBack(): void
self::assertSame('Original exception', $e->getPrevious()->getMessage());
}
}

/**
* We are not sure if this can happen in real life scenario
*/
public function testItFailsDuringCommitBeforeTouchingDb(): void
{
$connection = new class (['memory' => true], new Driver\SQLite3\Driver()) extends Connection {
public function commit(): void
{
throw new Exception('Fail before touching the db');
}

public function rollBack(): void
{
throw new Exception('Rollback got triggered');
}
};

$this->expectExceptionMessage('Rollback got triggered');
$connection->transactional(static function (): void {
});
}
}

interface ConnectDispatchEventListener
Expand Down

0 comments on commit cf80105

Please sign in to comment.