ID:               20374
 Updated by:       [EMAIL PROTECTED]
 Reported By:      [EMAIL PROTECTED]
-Status:           Open
+Status:           Closed
 Bug Type:         Feature/Change Request
 Operating System: Linux
 PHP Version:      4.2.2
-Assigned To:      
+Assigned To:      zak
 New Comment:

Thanks for the excellent and complete suggestion - it has been
implemented in CVS.


Previous Comments:
------------------------------------------------------------------------

[2002-11-11 17:45:38] [EMAIL PROTECTED]

While investigating server performance I noticed that my queries per
second was unusually high, as well as my SHOW STATUS commands in MySQL.
 After investigating through the source, I found the function
php_mysql_do_connect, which of course does connections.  Within there,
line 602 of php_mysql.c for 4.2.2, I found a mysql_stat() where the
MySQL connection was tested, and upon failure would reconnect.  My
suggestion for PHP, and what I am placing into my own source, is
instead of a mysql_stat, a mysql_ping is ran, if the API library is
above 3.22.3
(http://www.mysql.com/documentation/mysql/bychapter/index.html#News-3.22.3)
 Per the MySQL documentation:

http://www.mysql.com/documentation/mysql/bychapter/manual_Clients.html#mysql_ping


int mysql_ping(MYSQL *mysql) 

8.4.3.164 Description
Checks whether the connection to the server is working. If it has gone
down, an automatic reconnection is attempted. 

This function can be used by clients that remain idle for a long while,
to check whether the server has closed the connection and reconnect if
necessary. 

8.4.3.165 Return Values
Zero if the server is alive. Non-zero if an error occurred. 



Since the libmysqlclient API will already reconnect, there is no need
for PHP to do this internally.  If the API version is equal to or above
3.22.3, run a mysql_ping and upon failure, remove the connection from
the zend_hash and return failure.  If it is below 3.22.3 (which is 3
years old), run the old source.  I know this is not a huge change, but
it does make alittle difference in performance.  Below is a test.c
example of how this functions:


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mysql.h>

int main (void) {
        MYSQL mysql_connect;
        MYSQL_RES *mysql_result;
        MYSQL_ROW mysql_row;
        my_ulonglong id;
        int c;

        mysql_init(&mysql_connect);
       
mysql_real_connect(&mysql_connect,"localhost","root","inT99hB$",
"mysql", 0, NULL, 0);
        for (c = 0; c < 3; c++) {
                sleep(2);

                mysql_ping(&mysql_connect);
                printf("Error: %s\n", mysql_error(&mysql_connect));

                if (mysql_real_query(&mysql_connect,"SELECT
CONNECTION_ID()",sizeof("SELECT CONNECTION_ID()"))) {
                        printf("Error querying: %s\n",
mysql_error(&mysql_connect));
                } else {
                        mysql_result =
mysql_store_result(&mysql_connect);
                        mysql_row = mysql_fetch_row(mysql_result);

                        id = strtoul(mysql_row[0], NULL, 0);
                        mysql_free_result(mysql_result);

                        printf("Id is %lu\n", id);
                        mysql_kill(&mysql_connect, id);
                }
        }
}



Example of php_mysql.c
#if MYSQL_VERSION_ID > 32230 /* let mysql_ping check connection and
auto reconnect */
                        if(mysql_ping(le->ptr)) {
                                php_error(E_WARNING, "MySQL:  Link to
server lost, unable to reconnect");
                                zend_hash_del(&EG(persistent_list),
hashed_details, hashed_details_length+1);
                                efree(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
                        }
#else

... normal code...

#endif


Bests,

nickg

------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=20374&edit=1

Reply via email to