Bug #38107 mysqli creating multiple persistent connections not closing
Submitted: 14 Jul 2008 15:44 Modified: 14 Jul 2008 17:48
Reporter: john knowles Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server: General Severity:S2 (Serious)
Version:5.0.27 OS:Linux (fedora core 6)
Assigned to: CPU Architecture:Any
Tags: Connections, max connection, mysqli

[14 Jul 2008 15:44] john knowles
Description:
Creating multiple connections and not closing connections from mysqli php class.

database class:
[code]
<?php
//Database class to implement singleton pattern on top of mysqli
class database extends mysqli
{
//create a singleton instance
private $report;
private static $instance = NULL;

    //private constructor to prevent direct access
    private function __construct()
        {
            parent::__construct(constants::DBHOST, constants::DBUSER, constants::DBPASS, constants::DBNAME);
            if(mysqli_connect_errno())
                {
                    throw new Exception(mysqli_connect_error(), mysqli_connect_errno());
                }
        }
    //public create instance function to create singleton
    public function getinstance()
        {
            if(self::$instance === null)
                {
                    $c = __CLASS__;
                    self::$instance = new $c;
                }
            return self::$instance;
        }
    //prevent clone
    public function __clone()
        {
            throw new Exception("Cannot clone ".__CLASS__." class");
        }
        
    public function __destruct()
        {
            $this->close();
        }
}
?> 
[/code]

product class extract
[code]
<?php
class products
{
private $database;
private $paging;

//create a singleton instance
private static $instance = NULL;
	
	private function __construct()
		{
			$this->database = database::getinstance();
			$this->paging = paging::getinstance();
		}
		
	public function getinstance()
		{
			if(self::$instance === null)
				{
					$c = __CLASS__;
					self::$instance = new $c;
				}
			return self::$instance;
		}
	
	//prevent clone
	public function __clone()
		{
			throw new Exception("Cannot clone ".__CLASS__." class");
		}

	public function getproduct($id)
		{
			$id = $this->database->antisql($id);
			if ($results = $this->database->query("SELECT categories.name, categories.url, products.id, products.product_code, products.style_name, products.category_id, products.colour_code, products.colour_description, products.size_code, products.style_code, products.collection_code, products.product_group, products.net_weight, products.rrp, products.offer_price, products.stock, products.points, products.video, products.description, products.live FROM `products` INNER JOIN categories ON products.category_id=categories.id WHERE products.id='$id' AND products.live='1'"))
				{
					return $results;
				}
			return 0;
		}
}
?>
[/code]

view file
[code]
<?php
$products = products::getinstance();
$product = $products->getproduct($_GET['id']);
$row = $product->fetch_array(MYSQLI_ASSOC);
print_r($row);
[/code]

mysql> SHOW PROCESSLIST;
+------+-----------+---------------------------------------+-----------+---------+------+-------+------------------+
| Id   | User      | Host                                  | db        | Command | Time | State | Info             |
+------+-----------+---------------------------------------+-----------+---------+------+-------+------------------+
|  360 | tableusr002 | localhost                             | table002 | Sleep   | 7910 |       | NULL             | 
131 rows in set (0.00 sec)

How to repeat:
code used supplied above with process list extract within private comment
[14 Jul 2008 16:12] Sveta Smirnova
Thank you for the report.

Please describe way which you use to create multiple connections from PHP. For example, "while(1) $products = products::getinstance();" doesn't create more than 1 connection. Also please indicate version of PHP you use and provide infromation about MySQLi from phpinfo() output.
[14 Jul 2008 16:30] john knowles
PHP Version: 5.1.6

mysqli
MysqlI Support	enabled
Client API library version 	5.0.27
Client API header version 	5.0.27
MYSQLI_SOCKET 	/var/lib/mysql/mysql.sock

Directive	Local Value	Master Value
mysqli.default_host	no value	no value
mysqli.default_port	3306	3306
mysqli.default_pw	no value	no value
mysqli.default_socket	no value	no value
mysqli.default_user	no value	no value
mysqli.max_links	Unlimited	Unlimited
mysqli.reconnect	Off	Off

"Please describe way which you use to create multiple connections from PHP. For example,
"while(1) $products = products::getinstance();" doesn't create more than 1 connection."

re: Sorry for my ingnorance i do not understand what you mean by this.
[14 Jul 2008 16:41] Sveta Smirnova
Thank you for the feedback.

> re: Sorry for my ingnorance i do not understand what you mean by this.

In the initial description you wrote:

> Creating multiple connections and not closing connections from mysqli php class.

Please provide a way how you create multiple connections.
[14 Jul 2008 17:37] john knowles
The MySQL database created the multiple connections itself when only a single instance was created.

It seems to be a similar problem to this bug report but through different methods:
http://bugs.php.net/bug.php?id=39457
http://bugs.mysql.com/bug.php?id=33831

A database connection seemed to be made for each query/call to the mysql dbms rather than using persistent connections made by the mysqli library. It seems to be unable to reuse an already opened connection.
[14 Jul 2008 17:48] Sveta Smirnova
Thank you for the feedback.

Closed as duplicate of bug #33831. Please upgrade to PHP 5.3 as was suggested in the PHP bugs database.