|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-05-21 09:55 UTC] bengates at aliceadsl dot fr
Description:
------------
Hello,
I figured out that closing a PDO connection will only be effective if no PDOStatement are open.
If you prepare multiple statements, setting the $pdo instance to null will have no effect until you set any PDOStatement instance to null too.
Test script:
---------------
Examples :
<?php
$pdo = new Pdo("mysql:host=localhost", 'user', 'password');
$pdo = null;
sleep(10);
exit;
?>
=> Connection has sucessfully been closed and isn't visible in MySql processlist within the 10 seconds for the script to terminate.
<?php
$pdo = new Pdo("mysql:host=localhost", 'user', 'password');
$stmt = $pdo->prepare("SELECT * FROM mytable WHERE Id = ?");
$pdo = null;
sleep(10);
exit;
?>
=> Connection hasn't been closed and is visible (Sleep) in MySql processlist within the 10 seconds for the script to terminate.
<?php
$pdo = new Pdo("mysql:host=localhost", 'user', 'password');
$stmt = $pdo->prepare("SELECT * FROM mytable WHERE Id = ?");
$pdo = null;
$stmt = null;
sleep(10);
exit;
?>
=> Connection has sucessfully been closed and isn't visible in MySql processlist within the 10 seconds for the script to terminate.
Expected result:
----------------
Setting the pdo instance to null should really close mysql's connection, regardless of any PDOStatement instance still existing at this time.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 19:00:01 2025 UTC |
This would break legitimate code like this: <?php function foo() { $pdo = new Pdo("mysql:host=localhost", 'user', 'password'); return $pdo->prepare("SELECT * FROM mytable"); } $result = foo()->execute(); ?> The better approach would be to have a "close" method which disconnects, but those have proven to have issues. By PHP's short living nature this usually isn't needed. In the rare case where this is needed the developer has to handle his resources manually to make sure to unset all related instances.