Forum Moderators: coopster

Message Too Old, No Replies

retry on lost connection

         

jackvull

8:21 pm on Aug 24, 2009 (gmt 0)

10+ Year Member



if my site loses connection with the webserver will this reconnect or do I not need to repeat the connection code?

//connection to remote database to check and enter the details
$squid_conn = mysql_connect("myserver",
"user",
"pw");

while (!$squid_conn) {
sleep(10);
$squid_conn = mysql_connect("myserver",
"user",
"pw");
}
mysql_select_db("mydb", $squid_conn);

eelixduppy

11:15 pm on Aug 24, 2009 (gmt 0)



There is no way to ensure that you can connect with the server this way except for trying it. I guess you can do a loop and keep trying to connect until mysql_connect doesn't return false, but make sure you put a limit on that in case you can never connect to the server; you don't want to cause an infinite loop. Say, try 10 times and if that doesn't work print an error message for them to try back later.


$timeout = 10;
$i = 0;
while((($link = mysql_connect()) === false) ¦¦ $i >= $timeout++)
;

BTW, if you are experiencing continuous trouble connecting to your database server then you should seriously consider finding an alternate configuration.

jackvull

10:50 pm on Aug 28, 2009 (gmt 0)

10+ Year Member



Is this the correct way or will it sleep for 10seconds before connecting the 1st time round?

$timeout = 10;
$i = 1;
while((($link = mysql_connect()) === false) ¦¦ $i >= $timeout++)
{
sleep(10);
}

How could I add a die stament in there for the 10th retry?
It's usually on one retry in case it has lost some packets over the internet connection.