Commit 5fdedc4f authored by Benjamin Eberlei's avatar Benjamin Eberlei

Fix Connection#ping() to have clear behavior, refs GH-414

parent 8a18e986
...@@ -1491,15 +1491,29 @@ class Connection implements DriverConnection ...@@ -1491,15 +1491,29 @@ class Connection implements DriverConnection
} }
/** /**
* Ping the server! * Ping the server
*
* When the server is not available the method returns FALSE.
* It is responsibility of the developer to handle this case
* and abort the request or reconnect manually:
*
* @example
*
* if ($conn->ping() === false) {
* $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.
* *
* @return bool * @return bool
*/ */
public function ping() public function ping()
{ {
if ( ! $this->_isConnected) { $this->connect();
return false; // Don't connect if not done yet. It will be lazy on first use
}
if ($this->_conn instanceof PingableConnection) { if ($this->_conn instanceof PingableConnection) {
return $this->_conn->ping(); return $this->_conn->ping();
...@@ -1510,10 +1524,7 @@ class Connection implements DriverConnection ...@@ -1510,10 +1524,7 @@ class Connection implements DriverConnection
return true; return true;
} catch (DBALException $e) { } catch (DBALException $e) {
// As the underlying connection is unset, the next query will connect again thanks to the lazyness
$this->close();
}
return false; return false;
} }
}
} }
...@@ -218,14 +218,9 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -218,14 +218,9 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this->assertEquals($this->_conn->quote("foo", Type::STRING), $this->_conn->quote("foo", \PDO::PARAM_STR)); $this->assertEquals($this->_conn->quote("foo", Type::STRING), $this->_conn->quote("foo", \PDO::PARAM_STR));
} }
public function testPingDoesNotTriggerConnect() public function testPingDoesTriggersConnect()
{ {
$this->assertFalse($this->_conn->ping());
}
public function testPingReturnsTrueWhenConnectionIsPingedOrOpen()
{
$this->_conn->connect();
$this->assertTrue($this->_conn->ping()); $this->assertTrue($this->_conn->ping());
$this->assertTrue($this->_conn->isConnected());
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment