• <bdo id='8vqM4'></bdo><ul id='8vqM4'></ul>
      <legend id='8vqM4'><style id='8vqM4'><dir id='8vqM4'><q id='8vqM4'></q></dir></style></legend><tfoot id='8vqM4'></tfoot>
        <i id='8vqM4'><tr id='8vqM4'><dt id='8vqM4'><q id='8vqM4'><span id='8vqM4'><b id='8vqM4'><form id='8vqM4'><ins id='8vqM4'></ins><ul id='8vqM4'></ul><sub id='8vqM4'></sub></form><legend id='8vqM4'></legend><bdo id='8vqM4'><pre id='8vqM4'><center id='8vqM4'></center></pre></bdo></b><th id='8vqM4'></th></span></q></dt></tr></i><div id='8vqM4'><tfoot id='8vqM4'></tfoot><dl id='8vqM4'><fieldset id='8vqM4'></fieldset></dl></div>

        <small id='8vqM4'></small><noframes id='8vqM4'>

        如何使用 C API 为 MySQL 查询设置超时

        时间:2023-08-20
        <tfoot id='no06G'></tfoot>
          <bdo id='no06G'></bdo><ul id='no06G'></ul>
            1. <i id='no06G'><tr id='no06G'><dt id='no06G'><q id='no06G'><span id='no06G'><b id='no06G'><form id='no06G'><ins id='no06G'></ins><ul id='no06G'></ul><sub id='no06G'></sub></form><legend id='no06G'></legend><bdo id='no06G'><pre id='no06G'><center id='no06G'></center></pre></bdo></b><th id='no06G'></th></span></q></dt></tr></i><div id='no06G'><tfoot id='no06G'></tfoot><dl id='no06G'><fieldset id='no06G'></fieldset></dl></div>
                <tbody id='no06G'></tbody>
            2. <small id='no06G'></small><noframes id='no06G'>

                  <legend id='no06G'><style id='no06G'><dir id='no06G'><q id='no06G'></q></dir></style></legend>

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

                  问题描述

                  我知道这里有很多类似的问题,也有很多结果,当我用谷歌搜索时,但没有一个能回答我的问题.我读了这个,这个,这个 和 this 但它们都不适合我.我不谈论任何锁,我不想使用 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 版本:

                  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

                  至少,有没有办法取消查询?我可以作为不同的线程启动一个计时器,但是当它到期时..我可以以某种方式取消查询吗?

                  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?

                  推荐答案

                  好的,我找到了解决方案.感谢 WillPRR(我的同事).

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

                  无法在每个查询上启动一个新线程,因为这是一个实时应用程序,应该每秒处理 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).

                  此外,无法通过库终止连接,也无法取消/终止查询,因为问题出在数据库服务器上..

                  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( FAILURE ) 更好:这是相关问题:如何在 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.

                  重要提示:(感谢 Will) - 事实证明,我们的 MySQL 库包装器具有故障安全"标志,因此在关闭的套接字(或其他严重错误)上,它会尝试以解决"问题,因此在我的情况下,它会自行重新打开套接字.所以,我刚刚关闭了这个选项,现在一切都很好 - 执行因异常而终止 - 这是执行此操作的最软"方式.
                  这当然应该通过另一个线程来完成 - 例如一个计时器.

                  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 秒..
                  此外,另一件重要的事情是,这些超时(与任何其他选项一样)必须设置 after mysql_initbefore> mysql_connectmysql_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 查询设置超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:了解 Table 和 Transaction API 之间的区别 下一篇:根据半径从地图数据库中选择点

                  相关文章

                  最新文章

                    <tfoot id='okOBi'></tfoot>

                    <small id='okOBi'></small><noframes id='okOBi'>

                    1. <i id='okOBi'><tr id='okOBi'><dt id='okOBi'><q id='okOBi'><span id='okOBi'><b id='okOBi'><form id='okOBi'><ins id='okOBi'></ins><ul id='okOBi'></ul><sub id='okOBi'></sub></form><legend id='okOBi'></legend><bdo id='okOBi'><pre id='okOBi'><center id='okOBi'></center></pre></bdo></b><th id='okOBi'></th></span></q></dt></tr></i><div id='okOBi'><tfoot id='okOBi'></tfoot><dl id='okOBi'><fieldset id='okOBi'></fieldset></dl></div>
                        <bdo id='okOBi'></bdo><ul id='okOBi'></ul>
                      <legend id='okOBi'><style id='okOBi'><dir id='okOBi'><q id='okOBi'></q></dir></style></legend>