Doc #69730 [Com]: PDO: incorrect description of how to close the connection to the database
| From: | 37xzxz at gmail dot com | Date: | Fri, 29 May 2015 15:53:36 +0000 |
| Subject: | Doc #69730 [Com]: PDO: incorrect description of how to close the connection to the database | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-12380@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69730&edit=1
ID: 69730
Comment by: 37xzxz at gmail dot com
Reported by: 37xzxz at gmail dot com
Summary: PDO: incorrect description of how to close the
connection to the database
Status: Feedback
Type: Documentation Problem
Package: Documentation problem
PHP Version: 5.6.9
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
I think documentation should be updated to actual state. cappytoi posted his note 1 year ago. I
though that bug already exist somewhere here. But I failed to find it.
I reproduced this issue on php-5.5.14-Win32-VC11-x86 and php-5.6.9-Win32-VC11-x86:
I run php script and check MySQL using following query
SELECT
ID, USER, HOST, DB, COMMAND,
TIME, STATE, LEFT(INFO, 51200) AS Info FROM
information_schema.PROCESSLIST;
I see connection from host with php script during 10 seconds.
Previous Comments:
------------------------------------------------------------------------
[2015-05-29 14:31:14] cmb@php.net
Actually, cappytoi's note shouldn't be there at all. It is
explained that bug reports & missing documentation shall not be
posted as user contributed note, but rather a respective bug
report should be filed[1]. I'm reluctant to reject the note now,
as it already has been upvoted by a lot of users, so seems to be
important.
Anyhow, I'm neither able to reproduce the mentioned behavior of
your test script with PHP 5.5.25 nor 5.6.9. I can, however,
reproduce it with PHP 5.4.19. Did you really test this with PHP
5.6.9? If not, please do, and report back (don't forget to set the
status of this ticket to "open" after doing so) â there may
actually be a bug.
Otherwise "only" the documentation would have to be improved. The
respective statement is misleading at best:
| To close the connection, you need to destroy the object by
| ensuring that all remaining references to it are deleted--you do
| this by assigning NULL to the variable that holds the object.
Actually, assigning NULL to the variable that references the PDO
instance will not close the connection, if there are still other
references to this very instance (such as from a PDOStatement
instance, or from other variables referencing the same PDO
instance). These have to be removed also (for instance, by
assigning NULL to the variable that references the PDOStatement).
Of course, this can be automated by encapsulating the query
execution in a function (or method for that matter), in which case
the PDOStatement will be freed when the function returns.
[1] <https://php.net/manual/add-note.php?sect=pdo.connections.php#whatnottoenter>
------------------------------------------------------------------------
[2015-05-29 11:28:35] 37xzxz at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/pdo.connections
---
I can't find bug about what cappytoi wrote in his comment https://php.net/manual/en/pdo.connections.php#114822
"You shouldn't expect that your connection is closed when you set $dbh = null unless all
you do is just instantiating PDO class."
$stmt = null; // doing this is mandatory for connection to get closed
In fact this works if you use PDO::prepare and PDOStatement::execute.
If you use direct PDO::query and query contain some error "$stmt = null;" also
doesn't close the connection.
Test script:
---------------
<?php
$pdo = new PDO(
'mysql:host=localhost;port=3306;dbname=world',
'user',
'password'
);
try {
$stmt = $pdo->query('SELECT * FROM city1nonexisttable');
} catch (PDOException $e) {
$errorText = $e->getMessage();
var_dump($errorText);
}
$stmt = null;
$pdo = null;
sleep(10);
?>
Expected result:
----------------
The connection to the database will be closed after $pdo = null; as described in documentation.
Actual result:
--------------
The connection to the database remains active after $pdo = null; and hangs 10 seconds.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69730&edit=1