Bug #67317 [Opn->Wfx]: [PDO] $pdo = null doesn't close mysql connection if there are PDOStatements
| From: | johannes@php.net | Date: | Wed, 21 May 2014 10:12:19 +0000 |
| Subject: | Bug #67317 [Opn->Wfx]: [PDO] $pdo = null doesn't close mysql connection if there are PDOStatements | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-185881@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67317&edit=1
ID: 67317
Updated by: johannes@php.net
Reported by: bengates at aliceadsl dot fr
Summary: [PDO] $pdo = null doesn't close mysql connection if
there are PDOStatements
-Status: Open
+Status: Wont fix
Type: Bug
Package: PDO MySQL
Operating System: Ubuntu
PHP Version: 5.4.28
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2014-05-21 09:55:21] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67317&edit=1