-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Enhancement: expose ping() on pingable-connections. #414
Conversation
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.
Hello, thank you for creating this pull request. I have automatically opened an issue http://www.doctrine-project.org/jira/browse/DBAL-666 We use Jira to track the state of pull requests and the versions they got |
*/ | ||
public function ping() | ||
{ | ||
if (!($this->_conn instanceof PingableConnection)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
braces around the instanceOf check are not needed.
However, spaces around the !
are needed according to the coding standards
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ( ! ($this->_conn instanceof PingableConnection) ) {
or
if (false === ($this->_conn instanceof PingableConnection)) {
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
first one, but without the ending space, just:
if ( ! ($this->_conn instanceof PingableConnection)) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@beberlei are you using the brances around the instanceof ? I would have said
if ( ! $this->_conn instanceof PingableConnection) {
but maybe I'm doing a mix between the Doctrine CS and the Symfony ones (which are the one I use all the time), where it would be if (!$this->_conn instanceof PingableConnection) {
is it something which can be supported by other drivers too ? |
@stof I don't know — I think you can fake it with PDO, e.g.: try {
$pdo->query('SELECT 1');
} catch (\Exception $e) {
$this->connect_or_reconnect_code_here();
} |
I like the idea but I have not seen an implementation like mysqli::ping() in any other driver. At least we should find a way to make that possible in drivers that do not natively support such a method. Another idea would be to integrate that into the Connection interface instead of having a seperate PingableConnection interface? But only if that BC break is acceptable and if we find a way to implement that in other drivers, too. |
if (!($this->_conn instanceof PingableConnection)) { | ||
throw ConnectionException::unsupportedFeature('ping'); | ||
} | ||
return $this->_conn->ping(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New line before return
, please.
@deeky666 semantic versioniong and all, BC breaks for Doctrine 3.0 only, right? ;-) IMO, this could be refactored later when a release date is closer. What do you guys think of the |
The way to reconnect with PDO is to close the connection and instantiate a new one. So this would have to be implemented in /**
* Ping the server!
*
* @return bool
*/
public function ping()
{
if ( ! $this->_isConnected) {
return; // Don't connect if not done yet. It will be lazy on first use
}
if ($this->_conn instanceof PingableConnection) {
$this->_conn->ping(); // TODO handle the case of MySQLi failures in ping(). what are they ?
return;
}
try {
$this->query('SELECT 1');
} catch (DBALException $e) {
// As the underlying connection is unset, the next query will connect again thanks to the lazyness
$this->close();
}
} |
@stof Looks like a reasonable implementation. But instead of |
@deeky666 indeed, this is a good idea. I forgot that we have such method in the platform |
@stof Yes I was wondering myself that PDO cant possible implement this completly in the Driver. I would do it a bit differently by checking for the return value of ping on the driver and close if false, then have PDO do the SELECT1 and return false on exception. Also the check for isConnected has to return a boolean, as the API specifies bool as return value. |
@stof At least then this method in the platform would have an actual use case :D |
Hmm, thinking about this, I propose the following:
Thoughts? |
So PDOConnection didn't work. No 🍺 for me. |
* | ||
* @return bool | ||
*/ | ||
public function ping() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you implement the method, I would implement PingableConnection
on this class too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, this is the main connection and not connected (despite naming) with the real connections.
* license * author tags * doc blocks * new lines
Enhancement: expose ping() on pingable-connections.
🙇 Thanks to everyone for the help! |
@beberlei see the comments here: #414 (comment) the behavior of this method is inconsistent currently between MySQLi and others, with a different meaning for the |
@stof IMO, people who use mysqli will set the reconnect option and then it's consistent. It's up to the implementation in the code around it to handle the |
@till the point is that reutrn false for MySQLi or returning false for the other drivers has different meaning. false for other drivers in the current implement does not mean it cannot reconnect. It means it is not connected currently (but the next time it will need to connect, it will try it).
This means that the calling cannot handle Note that all these issues are related to a bad definition of the contracts in your 2 methods.
|
Should be consistent now, everything else is documented. |
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.