mysql 连接池超时问题

文章目录

  • mysql 连接池超时问题
    • 查看超时参数
    • 修改修改超时参数
    • 查看超时参数
    • 参考
  • MySQL中interactive_timeout和wait_timeout的区别
    • interactive_timeout
    • wait_timeout
    • 总结
    • 参考

mysql 连接池超时问题

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 289,991 milliseconds ago. The last packet sent successfully to the server was 289,992 milliseconds ago. is longer than the server configured value of ‘wait_timeout’. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property ‘autoReconnect=true’ to avoid this problem.

查看超时参数

mysql> select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout');
+---------------------+----------------+
| variable_name       | variable_value |
+---------------------+----------------+
| WAIT_TIMEOUT        | 60             |
| INTERACTIVE_TIMEOUT | 28800          |
+---------------------+----------------+
2 rows in set (0.00 sec)
mysql> show  global  variables like  'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout  | 60    |
+---------------+-------+
1 row in set (0.00 sec)

修改修改超时参数

60s太短了,导致数据还没返回,连接就关闭了,所以会出现上述问题,修改60为28800。

set global wait_timeout=28800;

查看超时参数

mysql>  select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout');
+---------------------+----------------+
| variable_name       | variable_value |
+---------------------+----------------+
| WAIT_TIMEOUT        | 28800          |
| INTERACTIVE_TIMEOUT | 28800          |
+---------------------+----------------+
2 rows in set (0.00 sec)

参考

spring连接数据源的方式,参数详解
http://www.blogjava.net/Alpha/archive/2009/03/29/262789.html

连接超时的C3P0官方解答建议
https://blog.csdn.net/frankcheng5143/article/details/50589264

MySQL中interactive_timeout和wait_timeout的区别

interactive_timeout

默认是28800,单位秒,即8个小时

The number of seconds the server waits for activity on an interactive connection before closing it. An interactive client is defined as a client that uses the CLIENT_INTERACTIVE option to mysql_real_connect(). See also wait_timeout.

wait_timeout

默认同样是28800s

The number of seconds the server waits for activity on a noninteractive connection before closing it. On thread startup, the session wait_timeout value is initialized from the global wait_timeout value or from the global interactive_timeout value, depending on the type of client (as defined by the CLIENT_INTERACTIVE connect option to mysql_real_connect()). See also interactive_timeout.

在线程启动时,会话wait_timeout值从全局wait_timeout值或全局interactive_timeout值初始化。

  1. interactive_timeout针对交互式连接,wait_timeout针对非交互式连接。所谓的交互式连接,即在mysql_real_connect()函数中使用了CLIENT_INTERACTIVE选项。
    说得直白一点,通过mysql客户端连接数据库是交互式连接,通过jdbc连接数据库是非交互式连接。
  2. 在连接启动的时候,根据连接的类型,来确认会话变量wait_timeout的值是继承于全局变量wait_timeout,还是interactive_timeout。

总结

  1. 控制连接最大空闲时长的wait_timeout参数。

  2. 对于非交互式连接,类似于jdbc连接,wait_timeout的值继承自服务器端全局变量wait_timeout。

    对于交互式连接,类似于mysql客户单连接,wait_timeout的值继承自服务器端全局变量interactive_timeout。

参考

https://www.cnblogs.com/ivictor/p/5979731.html

你可能感兴趣的:(mysql)