Skip to content

Commit 0cfd360

Browse files
authored
Merge pull request #3485 from morozov/ping-void
`Connection::ping()` will now return `void`
2 parents 56c936b + 0476c15 commit 0cfd360

File tree

6 files changed

+33
-28
lines changed

6 files changed

+33
-28
lines changed

UPGRADE.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Upgrade to 3.0
22

3+
## BC BREAK `Connection::ping()` returns `void`.
4+
5+
`Connection::ping()` and `PingableConnection::ping()` no longer return a boolean value. They will throw an exception in case of failure.
6+
37
## BC BREAK User-provided `PDO` instance is no longer supported
48

59
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()`.

lib/Doctrine/DBAL/Connection.php

+17-15
Original file line numberDiff line numberDiff line change
@@ -1557,38 +1557,40 @@ public function createQueryBuilder()
15571557
/**
15581558
* Ping the server
15591559
*
1560-
* When the server is not available the method returns FALSE.
1560+
* When the server is not available the method throws a DBALException.
15611561
* It is responsibility of the developer to handle this case
1562-
* and abort the request or reconnect manually:
1562+
* and abort the request or close the connection so that it's reestablished
1563+
* upon the next statement execution.
15631564
*
1564-
* @return bool
1565+
* @throws DBALException
15651566
*
15661567
* @example
15671568
*
1568-
* if ($conn->ping() === false) {
1569+
* try {
1570+
* $conn->ping();
1571+
* } catch (DBALException $e) {
15691572
* $conn->close();
1570-
* $conn->connect();
15711573
* }
15721574
*
15731575
* It is undefined if the underlying driver attempts to reconnect
15741576
* or disconnect when the connection is not available anymore
1575-
* as long it returns TRUE when a reconnect succeeded and
1576-
* FALSE when the connection was dropped.
1577+
* as long it successfully returns when a reconnect succeeded
1578+
* and throws an exception if the connection was dropped.
15771579
*/
1578-
public function ping()
1580+
public function ping() : void
15791581
{
15801582
$connection = $this->getWrappedConnection();
15811583

1582-
if ($connection instanceof PingableConnection) {
1583-
return $connection->ping();
1584+
if (! $connection instanceof PingableConnection) {
1585+
$this->query($this->getDatabasePlatform()->getDummySelectSQL());
1586+
1587+
return;
15841588
}
15851589

15861590
try {
1587-
$this->query($this->getDatabasePlatform()->getDummySelectSQL());
1588-
1589-
return true;
1590-
} catch (DBALException $e) {
1591-
return false;
1591+
$connection->ping();
1592+
} catch (DriverException $e) {
1593+
throw DBALException::driverException($this->_driver, $e);
15921594
}
15931595
}
15941596
}

lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,13 @@ private function setDriverOptions(array $driverOptions = [])
250250
/**
251251
* Pings the server and re-connects when `mysqli.reconnect = 1`
252252
*
253-
* @return bool
253+
* {@inheritDoc}
254254
*/
255-
public function ping()
255+
public function ping() : void
256256
{
257-
return $this->conn->ping();
257+
if (! $this->conn->ping()) {
258+
throw new MysqliException($this->conn->error, $this->conn->sqlstate, $this->conn->errno);
259+
}
258260
}
259261

260262
/**

lib/Doctrine/DBAL/Driver/PingableConnection.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ interface PingableConnection
1111
{
1212
/**
1313
* Pings the database server to determine if the connection is still
14-
* available. Return true/false based on if that was successful or not.
14+
* available.
1515
*
16-
* @return bool
16+
* @throws DriverException
1717
*/
18-
public function ping();
18+
public function ping() : void;
1919
}

tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,10 @@ public function testQuote()
264264

265265
public function testPingDoesTriggersConnect()
266266
{
267-
self::assertTrue($this->connection->ping());
267+
$this->connection->close();
268+
self::assertFalse($this->connection->isConnected());
269+
270+
$this->connection->ping();
268271
self::assertTrue($this->connection->isConnected());
269272
}
270273

tests/Doctrine/Tests/DBAL/Functional/Driver/Mysqli/ConnectionTest.php

-6
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ public function testUnsupportedDriverOption()
4848
$this->getConnection(['hello' => 'world']); // use local infile
4949
}
5050

51-
public function testPing()
52-
{
53-
$conn = $this->getConnection([]);
54-
self::assertTrue($conn->ping());
55-
}
56-
5751
/**
5852
* @param mixed[] $driverOptions
5953
*/

0 commit comments

Comments
 (0)