The last packet sent successfully to the server was 12,026,089 milliseconds ago. is longer than the

今天有一个项目在测试环境报错,框架是SpringCloud,数据库用的是腾讯云的数据库

报错的日志信息如下(去除公司信息):

### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 
The last packet successfully received from the server was 12,026,089 milliseconds ago.  The last packet sent successfully to the server was 12,026,089 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.
### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 12,026,089 milliseconds ago.  The last packet sent successfully to the server was 12,026,089 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.
; SQL []; The last packet successfully received from the server was 12,026,089 milliseconds ago.  The last packet sent successfully to the server was 12,026,089 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.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 12,026,089 milliseconds ago.  The last packet sent successfully to the server was 12,026,089 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.
        at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:98)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:82)
        at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
        at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
        at com.sun.proxy.$Proxy147.selectOne(Unknown Source)
        at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166)
        at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:82)
        at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 12,026,089 milliseconds ago.  The last packet sent successfully to the server was 12,026,089 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.
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990)
        at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3749)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2512)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
        at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1197)
        at sun.reflect.GeneratedMethodAccessor150.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:483)
        at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
        at com.sun.proxy.$Proxy204.execute(Unknown Source)
        at sun.reflect.GeneratedMethodAccessor150.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:483)
        at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59)
        at com.sun.proxy.$Proxy205.execute(Unknown Source)
        at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:63)
        at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
        at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
        at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
        at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
        at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
        at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
        at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
        at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
        at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:77)
        at sun.reflect.GeneratedMethodAccessor170.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:483)
        at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
        ... 116 common frames omitted

在错误日志中,提醒我们链接数据库的配置加上autoReconnect=true配置即可。
但是在springCloud的配置中心中,已经有这个配置了,所以再看看具体的原因。

java所有的数据库连接池,无论是C3P0,druid还是dbcp,包括springCloud自带的Hikari,都有一个类似于maxWait或者是maxIdleTime,这个配置的含义是当连接长时间没有向数据库发送请求的时候,就断开这个链接,避免对数据库连接造成浪费。这个时间设置不能乱设置。

下图就是一个springcloud配置中心的数据库配置
The last packet sent successfully to the server was 12,026,089 milliseconds ago. is longer than the_第1张图片

在数据库中运行查询语句:

show global variables like 'wait_timeout'; 

腾讯云查出的结果,默认是3600秒,1小时
The last packet sent successfully to the server was 12,026,089 milliseconds ago. is longer than the_第2张图片
看一下就知道问题在哪里了,腾讯云的数据库默认空闲时间是3600秒,1小时,而配置中心中空闲时间是18000秒,50小时,肯定有问题,数据库默认1个小时内,没有请求,他就会断开这个链接,而数据库连接池的配置是50个小时,所以他一直认为这个链接是有效的,所以当大于1个小时,没有请求调用数据库后,再次调用数据库,链接早就失效了,所以错误在这里。

**

解决方案:

**
1.登录腾讯云的控制台,进入mysql的实例列表,然后选则参数设置,修改参数interactive_timeout 和 wait_timeout ,都修改成最大。
这里说一下腾讯云的最大值是7400秒,2个多小时,阿里云好像是86400,24小时。这个可能有点不同

The last packet sent successfully to the server was 12,026,089 milliseconds ago. is longer than the_第3张图片
The last packet sent successfully to the server was 12,026,089 milliseconds ago. is longer than the_第4张图片
The last packet sent successfully to the server was 12,026,089 milliseconds ago. is longer than the_第5张图片
在这里插入图片描述
The last packet sent successfully to the server was 12,026,089 milliseconds ago. is longer than the_第6张图片
修改完成之后,去配置中心吧数据库连接池中的空闲时间修改下,例如:hiraki中的idle-timeout设置为3600 ,1个小时。这个值要比数据库默认的time_waitout的值要小。重启微服务即可。

你可能感兴趣的:(项目问题,mysql)