ZF-11084: isConnected() implementation in Zend_Db_Pdo_Abstract does not actually test connection

Issue Type: Bug Created: 2011-02-17T05:48:44.000+0000 Last Updated: 2012-11-16T18:41:30.000+0000 Status: Open Fix version(s): Reporter: Tomas Creemers (tomasc) Assignee: Ralph Schindler (ralph) Tags: - Zend_Db

Related issues: Attachments:


Zend_Db_Adapter_Abstract specifies all subclasses need to have the isConnected() function. The docblock indicates: "Test if a connection is active". The docblock in Zend_Db_Pdo_Abstract says the same. However, the implementation in Zend_Db_Pdo_Abstract (which is not overridden in any of its subclasses) is as follows:

<pre class="highlight">
public function isConnected()
    return ((bool) ($this->_connection instanceof PDO));

This only checks if $this->_connection was at one point a succesfully created PDO object (i.e. at one point there was a connection). It does not test the connection. If the connection drops, the PDO object and $this->_connection do not get unset. Therefore, this function will return true even if the adapter is not connected.

Possible fixes:

The only way I've found to reliably test a PDO connection is to use it to execute a statement (for example, "SELECT 1"; something even more generic would be even better). If this fails (carefully considering the current PDO::ATTR_ERRMODE-setting of the PDO object), it is an indication the connection might be severed. It is not fool proof, however, because it might also fail on some databases if the user does not have permission to execute select statements. (This does not fail on Mysql, however.)


Posted by Tomas Creemers (tomasc) on 2011-02-17T05:53:21.000+0000

added formatting tags around code snippet

Posted by Tomas Creemers (tomasc) on 2011-02-17T05:54:15.000+0000

correcting formatting tags for code snippet

Posted by Tomas Creemers (tomasc) on 2011-02-17T06:11:46.000+0000

adding additional info to the problems of the proposed fix

Posted by Rob Allen (rob) on 2012-11-16T18:41:30.000+0000

I think that the overhead of performing a SQL query against the db within isConnected() is too big a change at this stage in ZF's life.

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.