mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
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:
parent
4d0884b1d5
commit
6b13fc4001
@ -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
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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();
|
||||
|
27
src/database/tests/Stubs/ExceptionPDO.php
Normal file
27
src/database/tests/Stubs/ExceptionPDO.php
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user