php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #69730 PDO: incorrect description of how to close the connection to the database
Submitted: 2015-05-29 11:28 UTC Modified: 2016-07-01 09:13 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: 37xzxz at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.6.9 OS:
Private report: No CVE-ID: None
 [2015-05-29 11:28 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-29 14:31 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2015-05-29 14:31 UTC] 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 15:53 UTC] 37xzxz at gmail dot com
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.
 [2015-05-29 15:56 UTC] 37xzxz at gmail dot com
-Status: Feedback +Status: Open
 [2015-05-29 15:56 UTC] 37xzxz at gmail dot com
status update
 [2016-07-01 09:08 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=339536
Log: Fix #69730: PDO: incorrect description of how to close the connection to the database
 [2016-07-01 09:13 UTC] cmb@php.net
-Status: Open +Status: Closed
 [2016-07-01 09:13 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

I have documented that *all* references to the PDO object have to
be deleted, in order to close it. I have not documented the very
issue described in this report, because that is a bug which
already has been reported as bug #62847, and we usually don't
document bugs. I'm leaving cappytoi's note in the manual until the
bug is fixed, but going to remove Vicente's note as this doesn't
add useful information.

Note also, that there is alread request #62065 which suggests
adding an explicit disconnect() method.
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Mon Jun 18 11:01:59 2018 UTC