Processing of MySQL database connection timeout (wait_timeout) problem

Reference: http://blog.sina.com.cn/s/blog_6f3ff2c90100otay.html
Problem description:
This exception (com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure Last packet sent to the server was X ms ago), because the MySQL service is disconnected after being disconnected for a long time, the first request after disconnection will throw this exception.
■Reason
So since it is a connection timeout problem, it is necessary to go to MySQL to explore how the connection time is controlled. Open the MySQL console, run: show global variables like 'wait_timeout', check the MySQL system variables related to the connection time, and get the following results: 28800
    where wait_timeout is the variable responsible for timeout control, and its time is 28800s, which is 8 hours, it means that the MySQL service will be disconnected after an operation interval of 8 hours and needs to be reconnected again. There are also users who use jdbc.url=jdbc:mysql://localhost:3306/nd?autoReconnect=true in the URL to automatically restore the connection. Of course, this is possible, but it is applicable to MySQL4 and below. MySQL5 has been invalid, must adjust the system variables to control. The MySQL5 manual has the following descriptions for two variables:
    interactive_timeout: The number of seconds the server waits for activity before closing the interactive connection. Interactive clients are defined as clients that use the CLIENT_INTERACTIVE option in mysql_real_connect(). see also wait_timeout
    wait_timeout: The number of seconds the server waits for activity before closing a non-interactive connection. At thread startup, the session wait_timeout value is initialized according to the global wait_timeout value or the global interactive_timeout value, depending on the client type (defined by the connection option CLIENT_INTERACTIVE of mysql_real_connect() ), see also interactive_timeout
    In this way, the two variables are jointly controlled, Then they must be modified. Continue to go deeper into the value range of these two variables wait_timeout is 1-2147483 (Windows), 1-31536000 (linux), the value of interactive_time varies with wait_timeout, and their default values ​​are 28800.
■Solution
Method 1: Modify
    the configuration file MySQL system variables are controlled by the configuration file. When the configuration file is not configured, the system uses the default value. This 28800 is the default value. To modify it can only be modified in the configuration file. Under Windows, there is a mysql.ini configuration file under %MySQL HOME%/bin. After opening, add two variables and assign values ​​in the following locations. (modified to 388000 here)
    save and exit, restart the mysql service, it must be restarting the system service. You can see the modification result:
MySQL database connection timeout (wait_timeout) is handled
    in the configuration file under Linux system /etc/my.cnf. What needs to be said a little more is: which configuration file under windows needs to find mysql from the windows system service, open the properties, and see the parameter value in "executable file path", because it may be my.cnf instead of my. ini, this is due to the setup at install time, we may ignore it.
MySQL database connection timeout (wait_timeout) problem
method 2: set
set global wait_timeout=28800 in the console;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326851276&siteId=291194637