简体   繁体   English

如何在Symfony 1.4中关闭Doctrine连接

[英]How to close the Doctrine connection in Symfony 1.4

I'm using Symfony 1.4 for my web application which uses doctrine 1.2 to connect to my MySQL database. 我将Symfony 1.4用于Web应用程序,该Web应用程序使用准则1.2连接到MySQL数据库。 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. 我有一个Symfony任务,该任务首先使用原则查询来获取一些数据,然后执行非常耗时的非数据库操作(例如:永无休止的for循环),该操作不需要数据库连接。 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. 在执行非数据库操作时,我使用在MySQL上运行的SHOW PROCESSLIST命令检查了活动的mysql进程。 It still shows that the MySQL connection created by the Symfony application is not closed but in the Sleep state. 它仍然显示由Symfony应用程序创建的MySQL连接并未关闭,而是处于Sleep状态。

Is there any way to close the database connection permanently in Symfony. 有什么方法可以在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. 这很可能是因为Doctrine使用PDO,并且PDO在关闭MySQL连接之前需要取消所有引用,否则它将保持连接活动。 But I don't know how to clear the PDO references in Doctrine. 但是我不知道如何清除“ Doctrine”中的PDO参考。

That is some really old stuff you are working with. 那是您正在使用的一些非常古老的东西。 I'm not sure why you are getting things from the sfContext singleton. 我不确定为什么要从sfContext单例中获取信息。 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 . 这基本上是《 Doctrine1》手册中的内容

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM