1

I'm testing some mysql integration to my C code. I noticed that if mydomain.com is not available at the time the binary runs, it is stuck for little more than 2 minutes. I'd like to avoid this and set a timeout of 5 seconds maximum. How can I do that?

#include <my_global.h>
#include <mysql.h>

int main() {

    MYSQL *con = mysql_init(NULL);

    if (con == NULL) {
        fprintf(stderr, "%s\n", "debug: could not initialize database");
        exit(1);
    }

    if (mysql_real_connect(con, "mydomain.com", "testuser", 
            "testuserpwd", "db_test", 0, NULL, 0) == NULL) {
        exit(1);
    }
    ...
    mysql_close(con);

}
geohei
  • 696
  • 4
  • 15
  • 3
    It is documented in the manual: http://dev.mysql.com/doc/refman/5.7/en/mysql-options.html – Klas Lindbäck Jan 09 '17 at 14:33
  • 1
    Oh! And I think `mysql_error(con)` after proving that `con == NULL` is not good. If you can't initialize MySQL then it's not mysql's fault and thus, there will be "no error" as to what mysql knows so far. – Iharob Al Asimi Jan 09 '17 at 14:39
  • And also, [read this post](http://stackoverflow.com/a/2213644/1983495) – Iharob Al Asimi Jan 09 '17 at 14:44
  • @Klas - Thanks for your answer. Please correct me if I missed something. Your doc link refers to timeouts on the MySQL server side. However I'm talking about the failure of a client to connect to the MySQL server. Something different. – geohei Jan 09 '17 at 14:56
  • 1
    @Iharob - If you are referring to not freeing memory, the code snippet above was not complete. I added the `mysql_close(con);` now. – geohei Jan 09 '17 at 15:04
  • 1
    I meant one before `exit(1)`. – Iharob Al Asimi Jan 09 '17 at 15:10
  • 3
    @geohei Looks like client options to me. The descriptions says to: Call mysql_options() after mysql_init() and before mysql_connect(). Specifically, you should look at the option `MYSQL_OPT_CONNECT_TIMEOUT`. – Klas Lindbäck Jan 09 '17 at 15:10
  • @Iharob (1st comment) - Yes, you are right. This was nonsense. I changed the code, but nevertheless I don't now how to change the C code in order to avoid 2 minutes waiting for a non working connection. – geohei Jan 09 '17 at 15:17
  • 1
    Read the documentation, set the option before connection `MYSQL_OPT_CONNECT_TIMEOUT` – Iharob Al Asimi Jan 09 '17 at 15:51
  • @Klas - Right! It worked! Thanks a lot! Two more related questions. (1.) Shall I correct the code in my initial posting? (2.) Do I have to use an `unsigned int *` **variable** or can I also enter the value directly into the `mysql_options()` line? `mysql_options(con, MYSQL_OPT_CONNECT_TIMEOUT, (unsigned int *)5)` doesn't work. – geohei Jan 09 '17 at 16:19
  • 1
    1. Don't correct the code in the question. You can post the corrected code as an answer to your own question (preferably with a short explanation and the link to the doc page). 2. Since it requires the address of an `int` you should use a variable in this case. – Klas Lindbäck Jan 10 '17 at 07:40

1 Answers1

2

Here the code as it works as desired. In case the server is not available, the timout is now 5 seconds i.s.o. 2 minutes.

Thanks for the help!

#include <my_global.h>
#include <mysql.h>

int main() {

    MYSQL *con = mysql_init(NULL);
    unsigned int mysql_ct;

    mysql_ct = 5;

    if (con == NULL) {
        fprintf(stderr, "%s\n", "debug: could not initialize database");
        exit(1);
    }

    if (mysql_options(mysql_con, MYSQL_OPT_CONNECT_TIMEOUT, &mysql_ct)) {
        fprintf(stderr, "debug (mysql): [mysql_options] failed.");
    }

    if (mysql_real_connect(con, "mydomain.com", "testuser", 
            "testuserpwd", "db_test", 0, NULL, 0) == NULL) {
        exit(1);
    }
    ...
    mysql_close(con);

}
geohei
  • 696
  • 4
  • 15