MySQL Set query timeout method

Source: Internet
Author: User
Tags mysql code

First, in Libmysql, the Mysql_opt_read_timeout settings item is provided, and the Libmysql provides an API for setting related settings items.

The code is as follows Copy Code

Mysql_options:

int stdcall

Mysql_options (MYSQL *mysql,enum mysql_option option, const void *arg)

{

Dbug_enter ("Mysql_option");

Dbug_print ("Enter", ("option:%d", (int) option);

Switch (option) {

Case Mysql_opt_connect_timeout:

mysql->options.connect_timeout= * (uint*) arg;

Break

/** Read Timeout time * *

Case Mysql_opt_read_timeout:

mysql->options.read_timeout= * (uint*) arg;

Break

Case Mysql_opt_write_timeout:

mysql->options.write_timeout= * (uint*) arg;

Break

Case Mysql_opt_compress:

Mysql->options.compress= 1;

/* the following omitted * * *


However, unfortunately, only the mysqli extension, the mysql_options completely exposed to PHP:

The code is as follows Copy Code

Php_function (mysqli_options)

{

/** have omitted * *

Switch (Z_TYPE_PP (mysql_value)) {

/** without any restrictions, direct transmission to mysql_options * *

Case is_string:

ret = Mysql_options (Mysql->mysql, Mysql_option, Z_strval_pp (Mysql_value));

Break

Default

CONVERT_TO_LONG_EX (Mysql_value);

L_value = z_lval_pp (Mysql_value);

ret = Mysql_options (Mysql->mysql, Mysql_option, (char *) &l_value);

Break

}

Return_bool (!ret);

}

But since mysqli does not export this constant, we need to look at the MySQL code, get the actual value of Mysql_opt_read_timeout, and then call Mysql_options directly:

The code is as follows Copy Code

Enum Mysql_option

{

Mysql_opt_connect_timeout, Mysql_opt_compress, Mysql_opt_named_pipe,

Mysql_init_command, Mysql_read_default_file, Mysql_read_default_group,

Mysql_set_charset_dir, Mysql_set_charset_name, Mysql_opt_local_infile,

Mysql_opt_protocol, Mysql_shared_memory_base_name, Mysql_opt_read_timeout,

Mysql_opt_write_timeout, Mysql_opt_use_result,

Mysql_opt_use_remote_connection, Mysql_opt_use_embedded_connection,

Mysql_opt_guess_connection, Mysql_set_client_ip, Mysql_secure_auth,

Mysql_report_data_truncation, Mysql_opt_reconnect,

Mysql_opt_ssl_verify_server_cert

};


As you can see, the mysql_opt_read_timeout is 11.

Now, we can set the query timeout:

<?php

The code is as follows Copy Code

$mysqli = Mysqli_init ();

$mysqli->options (one/*mysql_opt_read_timeout*/, 1);

$mysql->real_connect (* * *);

However, because there is a retry mechanism in the libmysql (try it once and try it twice), we eventually set the time-out threshold three times times the value we set.

In other words, if we set the Mysql_opt_read_timeout to 1, we end up with a timeout after 3s. In other words, the minimum timeout we can set today is 3 seconds ...

Although the big point, but always better than no, hehe

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.