Jump to content

Recommended Posts

Hi,

 

I have a script that starts out with establishing a connection:

$dbConnection = mysql_connect(.., .., ..);

mysql_select_db(.., $dbConnection);

 

Problem is that my script calls some functions - and these functions call other functions - and one of the sub-sub-functions has a sleep() for 10-20 seconds.

 

The sleep() causes my mysql connection to timeout (error on mysql_query() is "MySQL server has gone away"). If I remove the sleep() I can run the script without any errors.

 

Unfortunately removing the sleep() in production isn't an option, so what can I do to avoid the timeouts?

 

Thanks a lot,

Phixx

Link to comment
https://forums.phpfreaks.com/topic/162532-connection-timeout-due-to-sleep/
Share on other sites

if I don't give them a short break once in a while

Ummm. Computers don't get tired. They can perform the same task indefinitely. As always, you need to find and fix the problem that lead you to believe you must insert the seep(). If you have a timing issue, you can probably solve that with some handshaking/conditional logic.

So there is no way to keep the connection alive longer, so it doesn't time out? Or a way to automatically reconnect if the server has gone away?

Not from a single-threaded script, no... but that's well outside the scope of the mysql board.

I got an idea:

 

Make a global variable, $dbConnection

 

Wrap sleep in disconnect/connect, like this:

mysql_close($dbConnection);

 

sleep(20);

 

$dbConnection = mysql_connect(...);

mysql_select_db(..., $dbConnection);

 

Problem is that mysql_error still returns "MySQL server has gone away" :(

 

Why is that?

 

Thanks,

phixx

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.