How to set a timeout for MySQL query using C API

前端 未结 4 1510
执笔经年
执笔经年 2021-01-02 15:33

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

4条回答
  •  青春惊慌失措
    2021-01-02 16:28

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

    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..

    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.

    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.

    EDIT: 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.

提交回复
热议问题