服务器有时会超时 [英] Server Timing Out Sometimes

查看:88
本文介绍了服务器有时会超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在连接我的在线mySql数据库,我遇到的问题是我所在的共享服务器习惯于超时并断开连接,这干扰了我的程序.有没有一种方法可以编写代码,如果发生这种情况,程序应该继续执行吗?我正在寻找从在线数据库返回的1或0.我在GoDaddy上.这是代码.谢谢.

I''m conecting to my online mySql database, the problem I am having is that the shared server I am on has a habit of timing out and dropping connections which is interupting my program. Is there a way to write in code, if this happens the program should continue? I am looking for either a 1 or a 0 to come back from the online database. I''m on GoDaddy. Here is the code. Thank you.

(mysql_real_connect(conn,"xxx.db.xxxx.hostedresource.com","xxx","xxx","xxx",0,NULL,0) !=0);
char queryString[1024];
sprintf(queryString, "SELECT COUNT(*) FROM tblURLIP WHERE IP = '%s' AND IPStatus = '1' AND IPType = '3' AND IPMax ='0'",ipSrc == NULL ? "0" : ipSrc);
mysql_query(conn, queryString);
my_ulonglong i = 0;
res_set = mysql_store_result(conn);
my_ulonglong numrows = mysql_num_rows(res_set);
LEGIT = mysql_fetch_row(res_set);

推荐答案

回到您的其他问题
Back at your other question Unhandled Exception / Access Violation[^] I told you that you needed to handle all the error conditions and not just rely on the record set containing 0. Now you''re back with the exact same program and still no handing of error conditions.

If you are doing calls, you need to realize that every call can FAIL miserably and you need to handle the failure return from the function. Only if it SUCCEEDS can you look at the data to see if the data says everything is OK. In other words, you need to deal with two types of FAILURES, 1) the overall operating system telling you that your call didn''t work. 2) the application (mysql) telling you that the call worked but the data returns a different result than you expected. Deal with both.

Once you handle errors, you can decide how to make the program continue / recover or not.

Added an Example

The documentation for mysql_query says it can return non-zero if any number of errors occur, including CR_SERVER_GONE_ERROR or CR_SERVER_LOST. You''ll need code like this (and don''t try to paste it, I''m just typing it in, no compiler here)
int q_result = mysql_query(conn, queryString);
if (q_result != 0) // query failed
{
  // do something intelligent with the error case, you probably have to close and re-establish the
  // connection too and that might not work right away because the server may be rebooting and
  // that takes to to complete.  Also, there may be a local hiccup in the connection to the network
  // (damn Verizon - that''s my carrier, you can bitch about your own provider in your own code).
}
res_set = mysql_store_result(conn);  // now that we know the query worked, lets get the results
if (res_set == NULL)
{
  // store result says there was an error, now I have to deal with that as well.  Why would the
  // query work and the store result fail?  Don''t know, ask the mysql developers.  I''m just trying to deal with it.
  // do more intelligent error handling here
}
// now it''s safe to pull data from the res_set using other mysql calls.


这篇关于服务器有时会超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆