MySQL automatically disconnects the connection solution after the idle time of MySQL connection exceeds 8 hours

Source: http://www.jb51.net/article/32284.htm
http://www.jb51.net/article/64935.htm
Comment:
Under MySQL's default settings, when a connection is idle for more than 8 hours , MySQL will disconnect the connection, and the c3p0 connection pool will think that the disconnected connection is still valid.

Assuming your database is mysql, if the data source is not configured properly, the classic "8 hour problem" may occur. The reason is that by default, if a connection is found to be idle for more than 8 hours, it will automatically close the connection on the database side. The data source does not know that the connection has been closed, and when it returns this useless connection to a dao, the dao will report an exception that the connection cannot be obtained.

    If the default configuration of dbcp is used, since the default value of the testOnBorrow property is true, the data source will check whether the connection is good before handing over the connection to dao. If there is a problem with the connection (closed on the database side), it will Take an other connection to dao. So there is no "8 hour problem". If the validity of the connection is checked every time the connection is handed over to the dao, it will cause performance problems in high-concurrency applications because it will require more database access requests.

    A recommended and efficient way is: set testOnBorrow to false, and set "testWhileIdle" to true, and then set the testBetweenEvictionRunsMillis value (less than 8 hours). Those connections closed by mysql can not be cleared out to avoid the "8-hour problem".

    Of course, mysql itself can also adjust the interactive-timeout (in seconds) configuration parameter to change the expiration time of idle connections. Therefore, when setting the timeBetweenEvictionRunsmMillis value, you must first know the maximum expiration time of mysql's idle connection.

    For the detection of valid connections by c3p0, please refer to the dbcp configuration method.

----
Under the default setting of MySQL, when a connection is idle for more than 8 hours, MySQL will disconnect the connection, and the c3p0 connection pool thinks that the disconnected connection is still valid. In this case, if the client code requests a connection from the c3p0 connection pool, the connection pool will return the invalid connection to the client, and the client will throw an exception when using


the There are three kinds:

1. Increase the value of MySQL's wait_timeout attribute.

Modify the /etc/mysql/my.cnf file and set it in the [mysqld] section:

# Set a connection to wait 8hours in idle status.
wait_timeout =86400
related parameters, red part
mysql> show variables like '%timeout%';
+ --------------------------+-------+
| Variable_name | Value |
+--------- -----------------+-------+
| connect_timeout | 5 |
| delayed_insert_timeout | 300 |
| innodb_lock_wait_timeout | 50 |
| interactive_timeout | 28800 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| slave_net_timeout | 3600 |
| wait_timeout | 28800 |
+--------------------------+-- -----+
At the same time, only one of these two parameters works. Which parameter works is related to the connection parameters specified by the user when connecting. By default, wait_timeout is used. My suggestion is to modify both parameters to avoid unnecessary trouble.

The default value of these two parameters is 8 hours (60*60*8=28800). I have tested changing these two parameters to 0, and the result is unexpected, the system automatically sets this value to . In other words, the value cannot be set permanently.
Set these two parameters to 24 hours (60*60*24=604800).
set interactive_timeout=604800;
set wait_timeout=604800;

2. Reduce the lifetime of connections in the connection pool to be less than the value of wait_timeout set in the previous item.
Modify the configuration file of c3p0 and set:

# How long to keep unused connections around(in seconds)
# Note: MySQL times out idle connections after 8hours(28,800seconds)
# so ensure this value is below MySQL idle timeout
cpool.maxIdleTime=25200
in Spring's configuration file:
copy the code as follows:

<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="maxIdleTime"value="${ cpool.maxIdleTime}"/>
<!--other properties -->
</bean>


3. Use connections in the connection pool periodically so that they are not disconnected by MySQL due to idle timeouts.
Modify the configuration file of c3p0 and set:

# Prevent MySQL raise exception after a long idle timecpool.preferredTestQuery='SELECT 1'cpool.idleConnectionTestPeriod=18000cpool.testConnectionOnCheckout=true
Modify the configuration file of Spring:
copy the code as follows:

<bean id=" dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="
<property name="idleConnectionTestPeriod" value="${cpool.idleConnectionTestPeriod}"/>
<property name="testConnectionOnCheckout" value="${cpool.testConnectionOnCheckout}"/>
<!--other properties --></bean>

Guess you like

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