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

Description

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:


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.)

Comments

added formatting tags around code snippet

correcting formatting tags for code snippet

adding additional info to the problems of the proposed fix

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.