如何使用C API来设置MySQL查询超时 [英] How to set a timeout for MySQL query using C API

查看:956
本文介绍了如何使用C API来设置MySQL查询超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多类似的问题在这里,也有很多的结果,当我谷歌,但他们没有回答我的问题。我读,的这个这个但没有人对我的作品。我不谈论任何锁,我不想这样做使用MySQL的C ++连接器,只需将C API。

I know there are lots of similar questions here, also there are lots of results, when I google it, but none of them answers my question. I read this, this, this and this but none of them works for me. I don't talk about any locks, I don't wanna do this using MySQL c++ connector, just the C API.

此外,什么是非常重要的位置:我这样做是Linux的。我为什么提这个?因为对于的mysql_options文档中:

Also, what is very important here: I do this on LINUX. Why I mention this? Because in the documentation for mysql_options:

MYSQL_OPT_READ_TIMEOUT - ...This option works only for 
TCP/IP connections and, prior to MySQL 5.0.25, only for Windows.

MYSQL_OPT_WRITE_TIMEOUT- ... This option works only for 
TCP/IP connections and, prior to MySQL 5.0.25, only for Windows

那么,有没有什么办法来设置版本查询超时,事先5.0.25?

So, is there any way to set a query timeout for versions, prior 5.0.25?

我的MySQL版本:

[root@xxx kiril]# mysql --version
mysql  Ver 14.12 Distrib 5.0.22, for redhat-linux-gnu (i686) using readline 5.0

编辑:至少,有没有办法取消查询?我可以启动一个定时器不同的线程,但它到期时..可我不知怎么取消查询?

At least, is there any way to cancel a query? I can start a timer as different thread, but when it expires.. can I cancel the query somehow?

推荐答案

好吧,我找到了解决..感谢 请问 PRR (我的同事)。

Okay, I found a solution.. Thanks to Will and PRR( my co-worker ).

我不能开始对每个查询一个新的线程,因为这是一个实时的应用程序,是应该处理1000+每秒的消息。(无论如何,这要感谢的 研究.. 的想法)。

I cannot start a new thread on each query, as this is a real-time application, that is supposed to process 1000+ messages per second..(anyway, thanks to R.. for the idea).

另外,这是不可能的,终止通过库的连接,也不取消/杀查询,因为这个问题是在DB服务器..

Also, it was not possible to terminate the connection through the library, nor to cancel/kill the query, as the problem was in the DB server..

和这里的蛮力解决方案,但仍然要好得多了 _exit(失败):下面是相关的问题:的如何强制Linux上关闭插座 - ?所以,我刚刚结束使用系统调用接口。

And here's a brute-force solution, but still much better that _EXIT( FAILURE ): Here's the related question: "How to force closing socket on Linux?" - so, I just closed the socket using a system call.

重要说明:(感谢威尔) - 我们的MySQL库包装了的万无一失的标志,这样,在关闭的插座(或其他严重错误),它试图原来, 解决的问题,所以它重新打开插座,其本身在我的情况。所以,我刚满关闭此选项,现在一切是好的 - 执行(Execute)被终止,因为异常的 - 这是最软的方式来做到这一点结果
这应通过另一个线程来完成,当然 - 一个计时器,例如

Important NOTE: (thanks Will) - It turned out, that our MySQL library wrapper has s "fail-safe" flag, so that on closed socket (or other critical error), it tries to "solve" the problem, so it reopens the socket, by itself, in my case. So, I just turned off this option and everything is fine now - the execute is terminated because of an exception - this is the "softest" way to do this.
This should be done through another thread, of course - a timer, for example.

编辑:超时真的工作版本5.0.25后。但是,至少在RHEL4和RHEL5,超时的两倍,由于某种原因!例如,如果某些超时设置为20秒,真正的超时时间为60秒〜..结果
此外,另一个重要的一点是,这些超时(如任何其他选项)必须设置 mysql_init 和< STRONG>之前 mysql_connect mysql_real_connect

The timeouts are really working for versions after 5.0.25. But, at least on RHEL4 and RHEL5, the timeouts are tripled for some reason! For example, if some of the timeouts is set to 20sec, the real timeout is ~60sec..
Also, another important thing is, that these timeouts(as any other options) MUST be set after mysql_init and before mysql_connect or mysql_real_connect.

这篇关于如何使用C API来设置MySQL查询超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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