Reset event dispatcher after db reconnect.

This commit is contained in:
李铭昕 2019-02-01 15:44:58 +08:00
parent b060f8fd49
commit 4a771490ae
2 changed files with 11 additions and 8 deletions

View File

@ -20,6 +20,7 @@ use Hyperf\DbConnection\Traits\DbConnection;
use Hyperf\Pool\Connection as BaseConnection;
use Hyperf\Pool\Exception\ConnectionException;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
class Connection extends BaseConnection implements ConnectionInterface, DbConnectionInterface
{
@ -88,6 +89,15 @@ class Connection extends BaseConnection implements ConnectionInterface, DbConnec
public function reconnect(): bool
{
$this->connection = $this->factory->make($this->config);
// Reset event dispatcher after db reconnect.
if ($this->container->has(EventDispatcherInterface::class)) {
if ($this->connection instanceof \Hyperf\Database\Connection) {
$dispatcher = $this->container->get(EventDispatcherInterface::class);
$this->connection->setEventDispatcher($dispatcher);
}
}
$this->lastUseTime = microtime(true);
return true;
}

View File

@ -19,7 +19,6 @@ use Hyperf\Pool\Pool;
use Hyperf\Pool\PoolOption;
use Hyperf\Utils\Arr;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
class DbPool extends Pool
{
@ -64,12 +63,6 @@ class DbPool extends Pool
protected function createConnection(): ConnectionInterface
{
$conn = new Connection($this->container, $this, $this->config);
if ($this->container->has(EventDispatcherInterface::class)) {
$dispatcher = $this->container->get(EventDispatcherInterface::class);
$conn->setEventDispatcher($dispatcher);
}
return $conn;
return new Connection($this->container, $this, $this->config);
}
}