Mysql network disconnection and reconnection

Source: Internet
Author: User

Mysql network disconnection and reconnection

Recently, I was working on a project's background daemon program. I needed multiple threads to operate mysql database services. To enhance the robustness of daemon, I unplugged the network cable after successful connection, then test the robustness of the daemon disconnect and reconnect. However, if the network is disconnected, re-plug the network cable and use mysql_real_conncet to reconnect to the system. The returned result is always false, that is, a reconnection failure, then I want to disconnect the network and release the mysql connection again (mysql_close (MYSQL), And re-Initialize mysql_init (MYSQL), And then use the mysql_real_conncet () function to connect, but the result is that the mysql_real_connect () error is reported, and the error message is insufficient memory, it is clear that my computer still has 5 GB of idle memory, but it is said that the memory is not enough...
After reading the information, I found a function called mysql_options. Combined with the mysql_ping function, mysql can be automatically reconnected after the connection is disconnected:
Use mysql_ping to automatically check for reconnection. Two functions are used: mysql_ping and mysql_options. The specific method is to use mysql_options before mysql_real_connect and after mysql_init. The usage is as follows:

 char value = 1; (void) mysql_init (&mysql); mysql_options(&mysql, MYSQL_OPT_RECONNECT, (char *)&value);

Combined with the use of Baidu Encyclopedia:
Mysql_optins
Mysql_ping


Then, before mysql_query, use mysql_ping to determine whether the connection is closed. If the connection is disconnected, the connection will be automatically reconnected, or if the query fails, execute the mysql_ping function, and then automatically reconnect to mysqlsever.
However, please note that automatic reconnection may also cause some side effects, as shown below:
* Transactions in any activity are rolled back, And the autocommit mode is set to reset.
* All table locks are released.
* All temporary tables are disabled (UNDO)
* Session variables are reinitialized to the values of the corresponding variables. the Session variable is reinitialized to the corresponding variable. This also affects those implicitly declared variables, set names. For example, using This also affects variables that are set implicitly by statements such as set names. This also affects variables and is a set of implicit declarations, such as specified NAMES.
* All user variable settings will be lost.
* Prepare a report and release it.
* The handle variable is disabled.
* LAST_INSERT_ID () is reset to 0.
* The lock obtained using GET_LOCK () is released.

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.