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配置中心的数据库配置
在这里插入图片描述

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

show global variables like 'wait_timeout'; 

腾讯云查出的结果,默认是3600秒,1小时
在这里插入图片描述
看一下就知道问题在哪里了,腾讯云的数据库默认空闲时间是3600秒,1小时,而配置中心中空闲时间是18000秒,50小时,肯定有问题,数据库默认1个小时内,没有请求,他就会断开这个链接,而数据库连接池的配置是50个小时,所以他一直认为这个链接是有效的,所以当大于1个小时,没有请求调用数据库后,再次调用数据库,链接早就失效了,所以错误在这里。

**

解决方案:

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

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
修改完成之后,去配置中心吧数据库连接池中的空闲时间修改下,例如:hiraki中的idle-timeout设置为3600 ,1个小时。这个值要比数据库默认的time_waitout的值要小。重启微服务即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"The last packet sent successfully to the server was 5,868 milliseconds ago"是一个错误提示信息,意味着最后一个数据包成功发送到服务器的时间距离现在已经过去了5,868毫秒。这个错误通常与数据库连接断开或连接超时相关。根据引用和引用提供的信息,有一种可能原因是连接断开导致的。解决这个问题的方法可以尝试以下几个步骤: 1. 检查网络连接:确保你的网络连接正常,并且可以成功连接到数据库服务器。可以尝试使用ping命令来测试与数据库服务器的连接。 2. 检查数据库配置:检查你的数据库连接配置是否正确,包括用户名、密码、数据库名称和服务器IP地址等。确保没有错误或拼写问题。 3. 检查数据库服务器状态:查看数据库服务器的状态,确认服务器正常运行,并且没有资源不足或其他故障。 4. 增加连接超时时间:如果连接超时导致了该错误,你可以尝试增加连接超时时间,可以在代码中设置连接参数或者在数据库服务器的配置文件中进行设置。 5. 进行保活操作:根据引用中提到的方法,可以定时进行简单的查询保活操作,以防止连接断开。 通过以上步骤,你可以尝试解决"The last packet sent successfully to the server was 5,868 milliseconds ago"这个错误提示。另外,如果你在使用特定的开发工具或框架,还可以查找相关文档或社区中是否有更具体的解决方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [以及 遇到的坑“The last packet sent successfully to the server was 0 m](https://download.csdn.net/download/weixin_38631729/14017081)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [mysql数据库连接报错The last packet sent successfully to the server was 0 milliseconds ago. The drive](https://blog.csdn.net/flyf000/article/details/126365343)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值