Skip to content

Commit 9bf6b5e

Browse files
committed
Fixed ping() does not works.
1 parent 61bca96 commit 9bf6b5e

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/MySQLReplication/Repository/MySQLRepository.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use MySQLReplication\BinLog\BinLogException;
99
use MySQLReplication\Exception\MySQLReplicationException;
1010

11-
class MySQLRepository implements RepositoryInterface
11+
class MySQLRepository implements RepositoryInterface, PingableConnection
1212
{
1313
private $connection;
1414

@@ -47,7 +47,7 @@ public function getFields(string $database, string $table): FieldDTOCollection
4747

4848
private function getConnection(): Connection
4949
{
50-
if (false === $this->connection->ping()) {
50+
if (false === $this->ping($this->connection)) {
5151
$this->connection->close();
5252
$this->connection->connect();
5353
}
@@ -95,4 +95,14 @@ public function getMasterStatus(): MasterStatusDTO
9595

9696
return MasterStatusDTO::makeFromArray($data);
9797
}
98+
99+
public function ping(Connection $connection): bool
100+
{
101+
try {
102+
$connection->executeQuery($connection->getDatabasePlatform()->getDummySelectSQL());
103+
return true;
104+
} catch (Exception $e) {
105+
return false;
106+
}
107+
}
98108
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MySQLReplication\Repository;
5+
6+
use Doctrine\DBAL\Connection;
7+
8+
/**
9+
* An interface for connections which support a "native" ping method.
10+
*/
11+
interface PingableConnection
12+
{
13+
/**
14+
* Pings the database server to determine if the connection is still
15+
* available. Return true/false based on if that was successful or not.
16+
*
17+
* @return bool
18+
*/
19+
public function ping(Connection $connection): bool;
20+
}

0 commit comments

Comments
 (0)