Commit 7d77a3fb authored by till's avatar till

Enhancement: expose ping() on pingable-connections.

This PR introduces a new PingableConnection interface and exposes
a ping method on \Doctrine\DBAL\Connection. For drivers where no
ping is supported, a \Doctrine\DBAL\ConnectionException is thrown.
parent d2a1d2a0
...@@ -30,6 +30,7 @@ use Doctrine\DBAL\Cache\ResultCacheStatement; ...@@ -30,6 +30,7 @@ use Doctrine\DBAL\Cache\ResultCacheStatement;
use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Cache\ArrayStatement; use Doctrine\DBAL\Cache\ArrayStatement;
use Doctrine\DBAL\Cache\CacheException; use Doctrine\DBAL\Cache\CacheException;
use Doctrine\DBAL\Driver\PingableConnection;
/** /**
* A wrapper around a Doctrine\DBAL\Driver\Connection that adds features like * A wrapper around a Doctrine\DBAL\Driver\Connection that adds features like
...@@ -1488,4 +1489,17 @@ class Connection implements DriverConnection ...@@ -1488,4 +1489,17 @@ class Connection implements DriverConnection
{ {
return new Query\QueryBuilder($this); return new Query\QueryBuilder($this);
} }
/**
* Ping the server!
*
* @return bool
*/
public function ping()
{
if (!($this->_conn instanceof PingableConnection)) {
throw ConnectionException::unsupportedFeature('ping');
}
return $this->_conn->ping();
}
} }
...@@ -57,4 +57,12 @@ class ConnectionException extends DBALException ...@@ -57,4 +57,12 @@ class ConnectionException extends DBALException
{ {
return new self("May not alter the nested transaction with savepoints behavior while a transaction is open."); 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));
}
} }
...@@ -20,11 +20,12 @@ ...@@ -20,11 +20,12 @@
namespace Doctrine\DBAL\Driver\Mysqli; namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\Driver\Connection as Connection; use Doctrine\DBAL\Driver\Connection as Connection;
use \Doctrine\DBAL\Driver\PingableConnection;
/** /**
* @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com> * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
*/ */
class MysqliConnection implements Connection class MysqliConnection implements Connection, PingableConnection
{ {
/** /**
* @var \mysqli * @var \mysqli
...@@ -207,4 +208,14 @@ class MysqliConnection implements Connection ...@@ -207,4 +208,14 @@ class MysqliConnection implements Connection
); );
} }
} }
/**
* Pings the server and re-connects when `mysqli.reconnect = 1`
*
* @return bool
*/
public function ping()
{
return $this->_conn->ping();
}
} }
<?php
namespace Doctrine\DBAL\Driver;
interface PingableConnection
{
public function ping();
}
\ No newline at end of file
...@@ -39,6 +39,12 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -39,6 +39,12 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this->getConnection(array('hello' => 'world')); // use local infile $this->getConnection(array('hello' => 'world')); // use local infile
} }
public function testPing()
{
$conn = $this->getConnection(array());
$conn->ping();
}
private function getConnection(array $driverOptions) private function getConnection(array $driverOptions)
{ {
return new \Doctrine\DBAL\Driver\Mysqli\MysqliConnection( return new \Doctrine\DBAL\Driver\Mysqli\MysqliConnection(
......
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