3
<?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.

aynber
  • 22,380
  • 8
  • 50
  • 63
Ian
  • 501
  • 2
  • 10
  • 20
  • What version of Mysql are you running? On php manual a user (comment #4) says the autmatic reconnection is disabled as for 5.0.13..Have a look there, you may find some clue http://php.net/manual/en/function.mysql-ping.php – Damien Pirsy Feb 01 '11 at 06:20
  • As I mention above, I have tried to query the sql statement. Just like mysql_ping, after I close the connection, the $connp or $conn are also close. – Ian Feb 01 '11 at 06:50
  • Are you using PHP 5.3? There are reportedly some issues with 5.3 and persistent connections. – Ian Gregory Feb 01 '11 at 08:05
  • Yes, I am using PHP Version 5.3.5. Do u mean that V5.3 has error?? So, what can I do? – Ian Feb 01 '11 at 08:12

1 Answers1

2

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:

  1. If the connection pointed by the resource is not a persistent one, the connection is closed.
  2. The resource is destroyed in every case.

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
Patrick Allaert
  • 1,751
  • 18
  • 44
  • Very Clear and thanks so much. But one more question, so can I use one persistent connection in different PHP?? – Ian Feb 01 '11 at 09:02
  • The goal of a persistent connection is to be reused across requests served by the same process. I'm not sure by what you mean with "different PHP". How are connections reused is entirely up to your setup, if using Apache with e.g. prefork MPM, every fork will have its own persistent connection. – Patrick Allaert Feb 01 '11 at 09:08
  • And, I noticed that the pconnect thread will not be changed even I didn't close the sql connection. So, does it just uses the same thread? If I have a lot of pconnect, are there a lot of thread which wasting my resources? Can I really close the pconnect except until it times out? And in my phpinfo, it said my timeout is 60s, however, after 60s, the thread id still remains, why? Thanks – Ian Feb 01 '11 at 09:11
  • The goal of a persistent connection is to be reused across requests served by the same process. So, that means after a single process, it should be close, isn't it? – Ian Feb 01 '11 at 09:12
  • 2
    One process will typically serves many requests. If you want to benefit of persistent connections, use them, but never use mysql_close() as it is pointless. If you want to spare resources, then avoid using persistent connections. In this case you will never have sleeping collections to your MySQL server but you will have the cost to connect at every single request. Note that persistent connections may have side effects. Read: http://stackoverflow.com/questions/1340859/which-is-better-mysql-connect-or-mysql-pconnect Personally, I **never** use mysql_close() – Patrick Allaert Feb 01 '11 at 10:00