PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Closing a Connection

Normally it is not necessary to close a database connection. PHP automatically cleans up all resources and the end of a request. Database extensions are designed to close the connection as the reference to the resource object is cleaned up.

However, if you have a long-duration PHP script that initiates many database connections, you might need to close the connection, to avoid exhausting the capacity of your RDBMS server. You can use the Adapter's closeConnection() method to explicitly close the underlying database connection.

Since release 1.7.2, you could check you are currently connected to the RDBMS server with the method isConnected(). This means that a connection resource has been initiated and wasn't closed. This function is not currently able to test for example a server side closing of the connection. This is internally use to close the connection. It allow you to close the connection multiple times without errors. It was already the case before 1.7.2 for PDO adapters but not for the others.

Example 217. Closing a Database Connection

<?php
$db
->closeConnection();

Does Zend_Db Support Persistent Connections?

Yes, persistence is supported through the addition of the persistent flag set to TRUE in the configuration (not driver_configuration) of an adapter in Zend_Db.

Example 218. Using the Persitence Flag with the Oracle Adapter

<?php
$db 
Zend_Db::factory('Oracle', array(
    
'host'       => '127.0.0.1',
    
'username'   => 'webuser',
    
'password'   => 'xxxxxxxx',
    
'dbname'     => 'test',
    
'persistent' => true
));

Please note that using persistent connections can cause an excess of idle connections on the RDBMS server, which causes more problems than any performance gain you might achieve by reducing the overhead of making connections.

Database connections have state. That is, some objects in the RDBMS server exist in session scope. Examples are locks, user variables, temporary tables, and information about the most recently executed query, such as rows affected, and last generated id value. If you use persistent connections, your application could access invalid or privileged data that were created in a previous PHP request.

Currently, only Oracle, DB2, and the PDO adapters (where specified by PHP) support persistence in Zend_Db.

Zend Framework