phixx Posted June 17, 2009 Share Posted June 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/162532-connection-timeout-due-to-sleep/ Share on other sites More sharing options...
trq Posted June 17, 2009 Share Posted June 17, 2009 Why on earth would you need sleep()? Quote Link to comment https://forums.phpfreaks.com/topic/162532-connection-timeout-due-to-sleep/#findComment-857878 Share on other sites More sharing options...
phixx Posted June 17, 2009 Author Share Posted June 17, 2009 Why on earth would you need sleep()? Because the function calls upon some resources that will break down if I don't give them a short break once in a while. Quote Link to comment https://forums.phpfreaks.com/topic/162532-connection-timeout-due-to-sleep/#findComment-857897 Share on other sites More sharing options...
trq Posted June 17, 2009 Share Posted June 17, 2009 Well, your server will time out (and mysql's connection will drop) if your requests take too long. They are your choices. Sounds to me like your script probably shouldn't be executed via a web server but rather a cron or some other process. Quote Link to comment https://forums.phpfreaks.com/topic/162532-connection-timeout-due-to-sleep/#findComment-857906 Share on other sites More sharing options...
PFMaBiSmAd Posted June 17, 2009 Share Posted June 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/162532-connection-timeout-due-to-sleep/#findComment-857910 Share on other sites More sharing options...
phixx Posted June 18, 2009 Author Share Posted June 18, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/162532-connection-timeout-due-to-sleep/#findComment-858569 Share on other sites More sharing options...
fenway Posted June 22, 2009 Share Posted June 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/162532-connection-timeout-due-to-sleep/#findComment-860962 Share on other sites More sharing options...
phixx Posted June 22, 2009 Author Share Posted June 22, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/162532-connection-timeout-due-to-sleep/#findComment-861110 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.