Fixed bug that database cannot work when disconnect caused by connection reset by mysql. (#5324)

* Fixed bug that database cannot work when disconnect caused by connection reset by mysql.

* Update CHANGELOG-3.0.md
This commit is contained in:
李铭昕 2023-01-12 18:04:57 +08:00 committed by GitHub
parent 4d0884b1d5
commit 6b13fc4001
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 8 deletions

View File

@ -3,6 +3,7 @@
## Fixed
- [#5318](https://github.com/hyperf/hyperf/pull/5318) Fixed bug that rate-limit cannot work when using php 8.1
- [#5324](https://github.com/hyperf/hyperf/pull/5324) Fixed bug that database cannot work when disconnect caused by connection reset by mysql.
## Added

View File

@ -29,6 +29,7 @@ use Hyperf\Database\Schema\Grammars\Grammar as SchemaGrammar;
use Hyperf\Utils\Arr;
use LogicException;
use PDO;
use PDOException;
use PDOStatement;
use Psr\EventDispatcher\EventDispatcherInterface;
use Throwable;
@ -639,7 +640,10 @@ class Connection implements ConnectionInterface
{
$this->transactions = 0;
$this->pdo = $pdo;
try {
$this->pdo = $pdo;
} catch (PDOException) {
}
return $this;
}
@ -652,7 +656,10 @@ class Connection implements ConnectionInterface
*/
public function setReadPdo($pdo)
{
$this->readPdo = $pdo;
try {
$this->readPdo = $pdo;
} catch (PDOException) {
}
return $this;
}

View File

@ -25,6 +25,7 @@ use Hyperf\Database\Query\Grammars\Grammar;
use Hyperf\Database\Query\Grammars\Grammar as QueryGrammar;
use Hyperf\Database\Query\Processors\Processor;
use Hyperf\Database\Schema\Builder;
use HyperfTest\Database\Stubs\ExceptionPDO;
use Mockery as m;
use PDO;
use PDOException;
@ -376,9 +377,11 @@ class DatabaseConnectionTest extends TestCase
$mock = $this->getMockConnection(['tryAgainIfCausedByLostConnection'], $pdo);
$mock->expects($this->once())->method('tryAgainIfCausedByLostConnection');
$method->invokeArgs($mock, ['', [], function () {
throw new QueryException('', [], new Exception());
}]);
$method->invokeArgs($mock, [
'', [], function () {
throw new QueryException('', [], new Exception());
},
]);
}
public function testRunMethodNeverRetriesIfWithinTransaction()
@ -395,9 +398,11 @@ class DatabaseConnectionTest extends TestCase
$mock->expects($this->never())->method('tryAgainIfCausedByLostConnection');
$mock->beginTransaction();
$method->invokeArgs($mock, ['', [], function () {
throw new QueryException('', [], new Exception());
}]);
$method->invokeArgs($mock, [
'', [], function () {
throw new QueryException('', [], new Exception());
},
]);
}
public function testFromCreatesNewQueryBuilder()
@ -452,6 +457,24 @@ class DatabaseConnectionTest extends TestCase
$this->assertSame($connection, $schema->getConnection());
}
public function testThrowExceptionWhenPDODestruct()
{
$connection = $this->getMockConnection(pdo: new ExceptionPDO(true));
$connection->setReadPdo(new ExceptionPDO(true));
$connection->disconnect();
$this->assertNull($connection->getPdo());
$this->assertNull($connection->getReadPdo());
$connection = $this->getMockConnection(pdo: new ExceptionPDO(true));
$connection->setPdo($pdo2 = new ExceptionPDO(false));
$this->assertSame($pdo2, $connection->getPdo());
}
protected function getMockConnection($methods = [], $pdo = null)
{
$pdo = $pdo ?: new DatabaseConnectionTestMockPDO();

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Database\Stubs;
use PDO;
use PDOException;
class ExceptionPDO extends PDO
{
public function __construct(public bool $throw)
{
}
public function __destruct()
{
$this->throw && throw new PDOException();
}
}