|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-12-25 16:24 UTC] sea128 at post dot cz
Description:
------------
Setting statement class with PDO object as parameter causes cyclic reference which prevent from garbage collection.
Test script:
---------------
// it is not sqlite bug, similar problem is with postgresql
$db = new PDO('sqlite::memory:');
// this line will prevent from garbage collection
$db->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('PDOStatement', array($db)));
// this line will not close connection
unset($db);
// this line will not close connection too
gc_collect_cycles();
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 03:00:02 2025 UTC |
I experience this same issue using PHP 5.5.9 and 5.6.11 (both on Ubuntu) and mysql (on Ubuntu or CentOS). I've set up a test to replicate the behavior (note the loop is to establish enough connections to hit the max_connections setting for the mysql server). In my tests, I get the expected output, which shows the last connection is still valid, but if the setAttribute line is un-commented, we will get the error message in the catch block. <?php class EPDOStatement extends \PDOStatement { } try { for ($x = 0; $x <= 1000; $x++) { $pdo = new \PDO("mysql:localhost", "test", "test"); // $pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, array("EPDOStatement", array($pdo))); if ($x !== 1000) { $pdo = null; } } } catch (Exception $e) { echo "Exception at number $x: " . $e->getMessage(); exit; } $stmt = $pdo->query("SHOW DATABASES"); print_r($stmt->fetchAll());