問題描述
我知道這里有很多類似的問題,也有很多結(jié)果,當(dāng)我用谷歌搜索時(shí),但沒有一個(gè)能回答我的問題.我讀了這個(gè),這個(gè),這個(gè) 和 this 但它們都不適合我.我不談?wù)撊魏捂i,我不想使用 MySQL C++ 連接器來做到這一點(diǎn),只是 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 上執(zhí)行此操作.為什么我提到這個(gè)?因?yàn)樵?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 之前的版本設(shè)置查詢超時(shí)?
So, is there any way to set a query timeout for versions, prior 5.0.25?
我的 MySQL 版本:
My MySQL version:
[root@xxx kiril]# mysql --version
mysql Ver 14.12 Distrib 5.0.22, for redhat-linux-gnu (i686) using readline 5.0
至少,有沒有辦法取消查詢?我可以作為不同的線程啟動(dòng)一個(gè)計(jì)時(shí)器,但是當(dāng)它到期時(shí)..我可以以某種方式取消查詢嗎?
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?
推薦答案
好的,我找到了解決方案.感謝 Will 和 PRR(我的同事).
Okay, I found a solution.. Thanks to Will and PRR( my co-worker ).
我無法在每個(gè)查詢上啟動(dòng)一個(gè)新線程,因?yàn)檫@是一個(gè)實(shí)時(shí)應(yīng)用程序,應(yīng)該每秒處理 1000 多條消息..(無論如何,感謝 R.. 的想法).
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).
此外,無法通過庫(kù)終止連接,也無法取消/終止查詢,因?yàn)閱栴}出在數(shù)據(jù)庫(kù)服務(wù)器上..
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..
這是一個(gè)蠻力解決方案,但比 _EXIT( FAILURE )
更好:這是相關(guān)問題:如何在 Linux 上強(qiáng)制關(guān)閉套接字?" - 所以,我只是使用系統(tǒng)調(diào)用關(guān)閉了套接字.
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.
重要提示:(感謝 Will) - 事實(shí)證明,我們的 MySQL 庫(kù)包裝器具有故障安全"標(biāo)志,因此在關(guān)閉的套接字(或其他嚴(yán)重錯(cuò)誤)上,它會(huì)嘗試以解決"問題,因此在我的情況下,它會(huì)自行重新打開套接字.所以,我剛剛關(guān)閉了這個(gè)選項(xiàng),現(xiàn)在一切都很好 - 執(zhí)行因異常而終止 - 這是執(zhí)行此操作的最軟"方式.
這當(dāng)然應(yīng)該通過另一個(gè)線程來完成 - 例如一個(gè)計(jì)時(shí)器.
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.
超時(shí)對(duì)于 5.0.25 之后的版本確實(shí)有效.但是,至少在 RHEL4 和 RHEL5 上,由于某種原因,超時(shí)時(shí)間增加了三倍!例如,如果某些超時(shí)設(shè)置為 20 秒,則實(shí)際超時(shí)為 ~60 秒..
此外,另一件重要的事情是,這些超時(shí)(與任何其他選項(xiàng)一樣)必須設(shè)置 after mysql_init
和 before> 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
.
這篇關(guān)于如何使用 C API 為 MySQL 查詢?cè)O(shè)置超時(shí)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!