Reputation: 501
<?php
$connp = mysql_pconnect("localhost", "root", "password");
echo mysql_stat($connp);
mysql_close($connp);
if(!mysql_ping($connp)){
echo " false";
}
echo "<br />";
$conn = mysql_connect("localhost", "root", "password");
echo mysql_stat($conn);
mysql_close($conn);
if(!mysql_ping($conn)){
echo " false";
}
echo "<br />";
?>
Hi all,
I have tested the php pconnect and connect.
The above is the code, the below is the result.
Uptime: 697914 Threads: 1 Questions: 1530 Slow queries: 0 Opens: 91 Flush tables: 1 Open tables: 0 Queries per second avg: 0.2 false
Uptime: 697914 Threads: 2 Questions: 1530 Slow queries: 0 Opens: 91 Flush tables: 1 Open tables: 0 Queries per second avg: 0.2 false
As you can see, both echo false. It should be that pconnect will not show false, isn't it? Since pconnect is waiting for timeout. But the fact is not like this.
Moreover, when I really close the $connp, it cannot query any sql statement. So, pconnect is exactly performing the same as connect.
I have turned on the persistent in php.inc already. Please tell me what wrong I have. Thanks all.
Upvotes: 3
Views: 2254
Reputation: 1752
In most case there is no reason to use mysql_close() combined with persistent connections, but below comes some explanation.
First of all, you should make the difference between a resource (a PHP variable, internally: a zval) and a connection (e.g. a TCP or socket connection aka a "link").
When you call mysql_connect(), a new connection is created every time and a new resource is returned. Multiple calls to mysql_connect() in the same script will create multiple connections and a corresponding resource will be returned.
When you call mysql_pconnect(), a new connection is created only if no active persistent connection is found in the current PHP process and a new resource is also returned every time. Multiple calls to mysql_pconnect() will return different resources which all point to the same connection.
mysql_close() will have an action on both the connection and the resource:
It does mean that using mysql_close() on a persistent connection, you have no way to use the connection anymore even if this one is still open, you can debug this with:
<?php
$connp = mysql_pconnect("localhost", "root", "password");
mysql_close($connp);
sleep(60);
?>
and looking in MySQL that the connection is still active:
SHOW PROCESSLIST;
If you had warnings enabled in your error_reporting, you would have seen a message like:
Warning: mysql_ping(): 4 is not a valid MySQL-Link resource in ... on line ...
The only way to use the previously created persistent connection is to create again a new resource which will point to it:
$connp = mysql_pconnect("localhost", "root", "password");
Using mysql_thread_id() on the resource will give you the MySQL connection identifier so that you can ensure mysql_close() does not close a persistent connection but only destroy the resource:
<?php
$conn = mysql_connect("localhost", "root", "password");
echo mysql_thread_id($conn), "\n";
mysql_close($conn);
$connp1 = mysql_pconnect("localhost", "root", "password");
echo mysql_thread_id($connp1), "\n";
mysql_close($connp1);
$connp2 = mysql_pconnect("localhost", "root", "password");
echo mysql_thread_id($connp2), "\n";
mysql_close($connp2);
?>
This will output something like:
61
62
62
Upvotes: 2