-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle lost connection during commit
- Loading branch information
Showing
3 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Functional; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Driver\PDO; | ||
use Doctrine\DBAL\Exception\ConnectionLost; | ||
use Doctrine\DBAL\Platforms\MySQLPlatform; | ||
use Doctrine\DBAL\Tests\FunctionalTestCase; | ||
|
||
use function sleep; | ||
|
||
use const E_ALL; | ||
use const E_WARNING; | ||
use const PHP_VERSION_ID; | ||
|
||
class TransactionTest extends FunctionalTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
if ($this->connection->getDatabasePlatform() instanceof MySQLPlatform) { | ||
return; | ||
} | ||
|
||
self::markTestSkipped('Restricted to MySQL.'); | ||
} | ||
|
||
public function testCommitFailure(): void | ||
{ | ||
$this->expectConnectionLoss(static function (Connection $connection): void { | ||
$connection->commit(); | ||
}); | ||
} | ||
|
||
public function testRollbackFailure(): void | ||
{ | ||
$this->expectConnectionLoss(static function (Connection $connection): void { | ||
$connection->rollBack(); | ||
}); | ||
} | ||
|
||
private function expectConnectionLoss(callable $scenario): void | ||
{ | ||
if (PHP_VERSION_ID < 70413 && $this->connection->getDriver() instanceof PDO\MySQL\Driver) { | ||
self::markTestSkipped('See https://bugs.php.net/bug.php?id=66528.'); | ||
} | ||
|
||
$this->connection->executeStatement('SET SESSION wait_timeout=1'); | ||
$this->connection->beginTransaction(); | ||
|
||
// during the sleep MySQL will close the connection | ||
sleep(2); | ||
|
||
// prevent the PHPUnit error handler from handling the "MySQL server has gone away" warning | ||
$this->iniSet('error_reporting', (string) (E_ALL & ~E_WARNING)); | ||
|
||
$this->expectException(ConnectionLost::class); | ||
$scenario($this->connection); | ||
} | ||
} |