diff --git a/src/Driver/MySQL/MySQLDriver.php b/src/Driver/MySQL/MySQLDriver.php index 8d2b6f7..5ea997a 100644 --- a/src/Driver/MySQL/MySQLDriver.php +++ b/src/Driver/MySQL/MySQLDriver.php @@ -67,6 +67,7 @@ protected function mapException(\Throwable $exception, string $query): Statement strpos($message, 'server has gone away') !== false || strpos($message, 'broken pipe') !== false || strpos($message, 'connection') !== false + || strpos($message, 'packets out of order') !== false || ((int)$exception->getCode() > 2000 && (int)$exception->getCode() < 2100) ) { return new StatementException\ConnectionException($exception, $query); diff --git a/tests/Database/Driver/MySQL/ExceptionsTest.php b/tests/Database/Driver/MySQL/ExceptionsTest.php index 071ab3e..4692989 100644 --- a/tests/Database/Driver/MySQL/ExceptionsTest.php +++ b/tests/Database/Driver/MySQL/ExceptionsTest.php @@ -11,6 +11,8 @@ namespace Spiral\Database\Tests\Driver\MySQL; +use Spiral\Database\Exception\StatementException\ConnectionException; + /** * @group driver * @group driver-mysql @@ -18,4 +20,24 @@ class ExceptionsTest extends \Spiral\Database\Tests\ExceptionsTest { public const DRIVER = 'mysql'; + + public function testPacketsOutOfOrderConsideredAsConnectionExceptionFromPHP74(): void + { + if (PHP_VERSION_ID < 70400) { + $this->markTestSkipped('Expecting PHP version >=7.4. Skipped due to ' . PHP_VERSION); + } + + // Prepare connection to generate "Packets out of order. Expected 1 received 0. Packet size=145" + // at the next query response + $this->database->query("SET SESSION wait_timeout=1")->fetch(); + sleep(1); + + try { + $result = $this->database->query('SELECT version() AS version')->fetchAll(); + $this->assertNotEmpty($result[0]['version'] ?? '', 'Expected result from second query'); + } catch (\RuntimeException $e) { + $this->assertInstanceOf(ConnectionException::class, $e); + return; + } + } }