Commit 11d160bd authored by till's avatar till

Enhancement: implement ping() for all drivers

 * h/t @stof and @deeky666
 * add two test cases for the general connection test
 * remove the ConnectionException method I added originally
parent dc6bad0d
......@@ -1497,10 +1497,22 @@ class Connection implements DriverConnection
*/
public function ping()
{
if ( ! ($this->_conn instanceof PingableConnection)) {
throw ConnectionException::unsupportedFeature('ping');
if ( ! $this->_isConnected) {
return false; // Don't connect if not done yet. It will be lazy on first use
}
return $this->_conn->ping();
if ($this->_conn instanceof PingableConnection) {
return $this->_conn->ping();
}
try {
$this->query($this->_platform->getDummySelectSQL());
return true;
} catch (DBALException $e) {
// As the underlying connection is unset, the next query will connect again thanks to the lazyness
$this->close();
}
return false;
}
}
......@@ -57,12 +57,4 @@ class ConnectionException extends DBALException
{
return new self("May not alter the nested transaction with savepoints behavior while a transaction is open.");
}
/**
* @return \Doctrine\DBAL\ConnectionException
*/
public static function unsupportedFeature($feature)
{
return new self(sprintf("The '%' feature is not supported by this connection/driver.", $feature));
}
}
......@@ -216,4 +216,15 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
$this->assertEquals($this->_conn->quote("foo", Type::STRING), $this->_conn->quote("foo", \PDO::PARAM_STR));
}
public function testPingDoesNotTriggerConnect()
{
$this->assertFalse($this->_conn->ping());
}
public function testPingReturnsTrueWhenConnectionIsPingedOrOpen()
{
$this->_conn->executeQuery($this->_conn->getDatabasePlatform()->getDummySelectSQL());
$this->assertTrue($this->_conn->ping());
}
}
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