1
votes

I'm using Symfony 1.4 for my web application which uses doctrine 1.2 to connect to my MySQL database. I have a Symfony task which first fetches some data using doctrine queries and then it does a very time-consuming non- database involved operation (ex: a never-ending for loop) which doesn't need a database connection. Since the latter operation doesn't need database connection to be open, I need to close the database connection after the database operations are done. So I tried the following code to close the database connection.

sfContext::createInstance($configuration);
sfContext::getInstance()->getDatabaseConnection('doctrine')->setAttribute(Doctrine_Core::ATTR_AUTO_FREE_QUERY_OBJECTS, true );

// Do the doctrine queries and fetch data

sfContext::getInstance()->getDatabaseManager()->shutdown();
gc_collect_cycles();

// Do the non-database operation

While the non-database operation is executing, I checked the active mysql processes using SHOW PROCESSLIST command running on MySQL. It still shows that the MySQL connection created by the Symfony application is not closed but in the Sleep state.

Is there any way to close the database connection permanently in Symfony.

Note: I found that the doctrine connection closes if the connection is closed before execuring the doctrine queries.

This is most probably because Doctrine uses PDO and PDO needs to unset all the references before making the MySQL connection close otherwise it keeps the connection active. But I don't know how to clear the PDO references in Doctrine.

1

1 Answers

2
votes

That is some really old stuff you are working with. I'm not sure why you are getting things from the sfContext singleton. You should be able to do this instead:

$conn = Doctrine_Manager::connection();
$conn->close();

This should close the connection, but leave it registered with the connection manager. This is basically right out of the Doctrine1 manual.

This will let you iterate through the connections and close them while also removing them from the manager:

$manager = Doctrine_Manager::getInstance();
foreach($manager as $conn) {
    $manager->closeConnection($conn);
}