Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Map mysqlnd Packets out of order error as ConnectionException (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
vokomarov committed Nov 3, 2021
1 parent b5eb675 commit 632488c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Driver/MySQL/MySQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions tests/Database/Driver/MySQL/ExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,33 @@

namespace Spiral\Database\Tests\Driver\MySQL;

use Spiral\Database\Exception\StatementException\ConnectionException;

/**
* @group driver
* @group driver-mysql
*/
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;
}
}
}

0 comments on commit 632488c

Please sign in to comment.