Skip to content

Commit

Permalink
Merge pull request #3485 from morozov/ping-void
Browse files Browse the repository at this point in the history
`Connection::ping()` will now return `void`
  • Loading branch information
Ocramius authored May 22, 2019
2 parents 56c936b + 0476c15 commit 0cfd360
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 28 deletions.
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Upgrade to 3.0

## BC BREAK `Connection::ping()` returns `void`.

`Connection::ping()` and `PingableConnection::ping()` no longer return a boolean value. They will throw an exception in case of failure.

## BC BREAK User-provided `PDO` instance is no longer supported

In order to share the same `PDO` instances between DBAL and other components, initialize the connection in DBAL and access it using `Connection::getWrappedConnection()->getWrappedConnection()`.
Expand Down
32 changes: 17 additions & 15 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1557,38 +1557,40 @@ public function createQueryBuilder()
/**
* Ping the server
*
* When the server is not available the method returns FALSE.
* When the server is not available the method throws a DBALException.
* It is responsibility of the developer to handle this case
* and abort the request or reconnect manually:
* and abort the request or close the connection so that it's reestablished
* upon the next statement execution.
*
* @return bool
* @throws DBALException
*
* @example
*
* if ($conn->ping() === false) {
* try {
* $conn->ping();
* } catch (DBALException $e) {
* $conn->close();
* $conn->connect();
* }
*
* It is undefined if the underlying driver attempts to reconnect
* or disconnect when the connection is not available anymore
* as long it returns TRUE when a reconnect succeeded and
* FALSE when the connection was dropped.
* as long it successfully returns when a reconnect succeeded
* and throws an exception if the connection was dropped.
*/
public function ping()
public function ping() : void
{
$connection = $this->getWrappedConnection();

if ($connection instanceof PingableConnection) {
return $connection->ping();
if (! $connection instanceof PingableConnection) {
$this->query($this->getDatabasePlatform()->getDummySelectSQL());

return;
}

try {
$this->query($this->getDatabasePlatform()->getDummySelectSQL());

return true;
} catch (DBALException $e) {
return false;
$connection->ping();
} catch (DriverException $e) {
throw DBALException::driverException($this->_driver, $e);
}
}
}
8 changes: 5 additions & 3 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,13 @@ private function setDriverOptions(array $driverOptions = [])
/**
* Pings the server and re-connects when `mysqli.reconnect = 1`
*
* @return bool
* {@inheritDoc}
*/
public function ping()
public function ping() : void
{
return $this->conn->ping();
if (! $this->conn->ping()) {
throw new MysqliException($this->conn->error, $this->conn->sqlstate, $this->conn->errno);
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/DBAL/Driver/PingableConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ interface PingableConnection
{
/**
* Pings the database server to determine if the connection is still
* available. Return true/false based on if that was successful or not.
* available.
*
* @return bool
* @throws DriverException
*/
public function ping();
public function ping() : void;
}
5 changes: 4 additions & 1 deletion tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ public function testQuote()

public function testPingDoesTriggersConnect()
{
self::assertTrue($this->connection->ping());
$this->connection->close();
self::assertFalse($this->connection->isConnected());

$this->connection->ping();
self::assertTrue($this->connection->isConnected());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ public function testUnsupportedDriverOption()
$this->getConnection(['hello' => 'world']); // use local infile
}

public function testPing()
{
$conn = $this->getConnection([]);
self::assertTrue($conn->ping());
}

/**
* @param mixed[] $driverOptions
*/
Expand Down

0 comments on commit 0cfd360

Please sign in to comment.