|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-05-18 20:26 UTC] b12 at bsdpower dot com
Description: ------------ Currently the advertised way of having pdo disconnect from the database is to assign "null" to the pdo handle. This may work acceptably well in tutorials, however in real life this approach is impractical. When testing with, say, phpunit (dbunit), the setup code creates a connection and gives a pdo instance to phpunit. What phpunit subsequently does with that instance and in particular how many times the pdo variable is copied and assigned to cyclical and/or permanently referenced structures, is not something that connection management code can control. Example code from phpbb: https://github.com/phpbb/phpbb3/blob/develop/tests/test_framework/phpbb_database_test_connection_manager.php#L38 - connect function is called by phpunit. Currently in phpbb tests the database connections are not closed by pdo. This requires for example postgresql and oracle databases to be configured to allow more concurrent database connections than we have tests in the test suite. You might be tempted to say that it's phpunit's fault for not closing database connections, or there is a bug in phpbb test code which results in connections not being closed. Consider how such a bug might be found. If I close a connection when I think it should no longer be used, and subsequently it is used, I will receive an error pointing to the responsible party. How would you diagnose the same bug if there is no way to close a database connection explicitly? Not to mention that disconnecting from the database is a core operation of any sane db api. There is no need to change existing behavior of connection closing on finalization. The disconnect method should be in addition to existing functionality. Expected result: ---------------- PDO should provide a method on connections to disconnect from the database. PatchesPHP72_Add_PDO_Close.patch (last revision 2018-05-31 09:34 UTC by cato at timeanddate dot com)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 19:00:01 2025 UTC |
Hello, Actually unset($pdo) or $pdo = null works well as soon as there aren't any PDOStatement object initialized somewhere. When working on prepared statements, unsetting the PDO instance has the following behaviour : $pdo = new PDO([...]); $stmt = $pdo->prepare("SELECT * FROM mytable WHERE mycolumn = :myvalue"); $stmt->bindValue(':myvalue', 'oh hi'); $stmt->execute(); $myvalue = $stmt->fetch(PDO::FETCH_COLUMN); unset($myvalue); // $myvalue == null unset($stmt); // $stmt == null unset($pdo); // $pdo == null sleep(30); // for 30 seconds, I can see the PDO connection is still alive on my MySql server, despite $pdo is null. This wouldn't happen if I didn't prepare a statement. Tested on PHP 5.3, PHP 5.4, PHP 5.5. Do you plan a fix for this ? To avoid hundreds of sleeping connections while parsing big files, PHP should really disconnect from the database when asked to.