Mysql_ping and MySQL persistent connection-go

Source: Internet
Author: User

First, if a persistent connection is used and no operations are performed on the database for a long time, the MySQL server closes the connection after the timeout value, when the client executes the query, it will get an error similar to "MySQL server has gone away.

A good solution is to use mysql_ping. After using mysql_real_connect to connect to the database, use mysql_options (& MySQL, mysql_opt_reconnect ,... To automatically reconnect. In this way, when the MySQL connection is lost, you can use mysql_ping to automatically reconnect to the database. If it is before MySQL 5.1.6, execute mysql_options (& MySQL, mysql_opt_reconnect ,... ), For MySQL
5.1.6 +, it is enough to execute one operation before connect.

A natural idea is to open a new thread to execute mysql_ping every certain time (for example, 20 seconds). In addition, it does not need to do anything. But obviously, this thread must share a MySQL connection with other threads and share the MySQL handle. Otherwise, it makes no sense to do so.

However, mysql_ping changes the return value of mysql_affected_rows. Therefore, it is best to add a mutex (preferably a read/write lock) to the MySQL handle ). When other threads are preparing to execute the query, they obtain the lock and release the lock after execution. The thread executing mysql_ping tries to obtain the lock before executing ping. If the acquisition fails, the thread continues sleep and stops this round of Ping.

Int mysql_options (MySQL * MySQL, Enum mysql_option option, const char * Arg)

Description
It can be used to set additional connection options and affect connection behavior. You can call this function multiple times to set several options.
Call mysql_options () after mysql_init () and before mysql_connect () or mysql_real_connect ().
The option parameter refers to the option you want to set. The ARG parameter is the option value. If the option is an integer, Arg should point to the integer value.

For detailed options of mysql_optinos, see the documentation. Only some common ones are listed here. For specific usage, see
Mysql-5.1 \ tests \ mysql_client_test.c

My_bool B = 0;
Mysql_options (* SQL, mysql_report_data_truncation, & B );

Mysql_options (lmysql, mysql_set_charset_name, "GBK"); // utf8

My_bool my_true = true;
Mysql_options (lmysql, mysql_opt_reconnect, & my_true );

Mysql_options (& MySQL, mysql_opt_compress, 0 );
Mysql_options (& MySQL, mysql_read_default_group, "ODBC ");

Related Article

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.