Search code examples
phpmysqlipersistent-connectioninteractive-mode

PHP & mysqli: What is the difference between persistent connection and interactive?


What are the differences between the following:

// Persistent connection
$mysqli->real_connect("p:" . $host, $user, $pass, $db, null, null, 0);

and

// Interactive connection
$mysqli->real_connect($host, $user, $pass, $db, null, null, MYSQLI_CLIENT_INTERACTIVE);

or even this one...

// Interactive AND persistent
$mysqli->real_connect("p:" . $host, $user, $pass, $db, null, null, MYSQLI_CLIENT_INTERACTIVE);

Solution

  • Just my interpretation of the docs...

    Persistent connections are kept open indefinitely. Non-persistent connections are not.

    The mysql_pconnect() docs are a little more clear than the real_connect() docs:

    mysql_pconnect() acts very much like mysql_connect() with two major differences.

    First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

    Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use ( mysql_close() will not close links established by mysql_pconnect()).

    This type of link is therefore called 'persistent'.

    Regarding MYSQLI_CLIENT_INTERACTIVE:

    MYSQLI_CLIENT_INTERACTIVE

    Allow interactive_timeout seconds (instead of wait_timeout seconds) of inactivity before closing the connection. The client's session wait_timeout variable will be set to the value of the session interactive_timeout variable.

    Normally, non-persistent connections are closed after the number of seconds specified by the wait_timeout variable. With this flag, they are instead closed after the number of seconds specified by the interactive_timeout variable.

    By my interpretation, that means that the MYSQLI_CLIENT_INTERACTIVE does not change the behavior of a persistent connection.