ZF-2781: closeConnection should return a boolean value not exceptions


In some cases you may want to make sure that all database connections are closed, but you may intend to reconnect them later. If you call close on a Database that does not have a connection established or the the connection is closed, an error will be generated.

There are 2 ways to solve this:

In the Zend_Db_Adapter_Mysqli there is a line that assumes that if closeConnection is called, the connection has been set.

public function closeConnection()
    $this->_connection = null;

If closeConnection is called twice this will generate an error. This should instead check that $this->_connection is set before it attempts to close it. If $this->_connection is not set, the value "false" should be returned.

Another solution is to expect the programmer to check that the connection is there first in their own code. This would require a new method called something like "isConnected" and that just checks that the connection is there. If it is not, it should return false without establishing a connection.


Please categorize/fix as needed.

What's wrong with using a {{try...catch}} block when calling a method that may throw an exception? It seems you are using {{close()}} in unintended ways, hence the exception being thrown.

That said, I do tend to agree that an exception should only be thrown if it is indeed an exceptional condition where processing cannot continue. This doesn't seem to be one of those cases. Instead, the implementation can be modified to gracefully handle the case where an exception is thrown without throwing an exception.

Still, this would be a backward-compatibility break that may or may not make it into the next major release.

Reassigning to Ralph since he's the new maintainer of Zend_Db

Same issue as ZF-5050

Fixed in ZF-5050