服务器有时会超时 [英] Server Timing Out Sometimes
本文介绍了服务器有时会超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在连接我的在线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);
推荐答案
回到您的其他问题 ^ ]我告诉您,您需要处理所有错误情况,而不仅仅是依赖包含0的记录集.完全相同的程序,仍然没有错误条件的处理.
如果要进行调用,则需要意识到每个调用都会失败,并且需要处理该函数的失败返回.只有成功,您才能查看数据以查看数据是否一切正常.换句话说,您需要处理两种类型的失败:1)整个操作系统告诉您您的呼叫无效. 2)应用程序(mysql)告诉您该调用有效,但是数据返回的结果与您预期的不同.应对两者.
处理错误后,您可以决定如何使程序继续/恢复.
添加了示例
mysql_query
的文档说,如果发生任何数量的错误(包括CR_SERVER_GONE_ERROR
或CR_SERVER_LOST
),则它可以返回非零值.您将需要这样的代码(不要尝试粘贴它,我只是在其中输入内容,此处没有编译器)
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 formysql_query
says it can return non-zero if any number of errors occur, includingCR_SERVER_GONE_ERROR
orCR_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屋!
查看全文