Spring boot2连接MySQL出现异常:The last packet successfully received from the server was x milliseconds ago

一、错误日志

2018-10-11 04:00:15,642 ERROR traceId:0 com.alibaba.druid.pool.DruidDataSource:1600 - discard connection   -- Exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 
  
 The last packet successfully received from the server was 7,214,506 milliseconds ago.  The last packet sent successfully to the server was 7,214,451 milliseconds ago. 
 	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
 	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
 	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
 	at java.lang.reflect.Constructor.newInstance(Unknown Source) 
 	at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) 
 	at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:988) 
 	at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3552) 
 	at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3452) 
 	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3893) 
 	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526) 
 	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673) 
 	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549) 
 	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861) 
 	at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1192) 
 	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3051) 
 	at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440) 
 	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3049) 
 	at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:167) 
 	at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:498) 
 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
 	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
 	at java.lang.reflect.Method.invoke(Unknown Source) 
 	at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59) 
 	at com.sun.proxy.$Proxy171.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 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
 	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
 	at java.lang.reflect.Method.invoke(Unknown Source) 
 	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) 
 	at com.sun.proxy.$Proxy123.selectList(Unknown Source) 
 	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230) 
 	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137) 
 	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75) 
 	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59) 
 	at com.sun.proxy.$Proxy124.getNoviceMediaId(Unknown Source) 
 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
 	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
 	at java.lang.reflect.Method.invoke(Unknown Source) 
 	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) 
 	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) 
 	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 
 	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) 
 	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
 	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) 
 	at com.sun.proxy.$Proxy126.getNoviceMediaId(Unknown Source) 
 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
 	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
 	at java.lang.reflect.Method.invoke(Unknown Source) 
 	at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) 
 	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) 
 	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81) 
 	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) 
 	at java.util.concurrent.FutureTask.run(Unknown Source) 
 	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source) 
 	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) 
 	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
 	at java.lang.Thread.run(Unknown Source) 
 Caused by: java.net.SocketException: Connection reset 
 	at java.net.SocketInputStream.read(Unknown Source) 
 	at java.net.SocketInputStream.read(Unknown Source) 
 	at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:101) 
 	at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:144) 
 	at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:174) 
 	at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3001) 
 	at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3462) 
 	... 66 more

二、错误分析:

  在MySql中查询:

mysql> show variables like 'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout  | 28800 |
+---------------+-------+
1 row in set (0.02 sec)
  • 从查询结果,可知一个连接(Connection)的等待时间是28800秒,即8小时。
  • 如果某个连接空闲超过8小时,则会断开该连接。
  • MySql连接池默认情况下并不对连接的有效性进行校验。
  • 如果程序使用一个已经断开的连接进行查询,则会报错如上所示。

三、解决方法

3.1 说明

解决此类问题有很多种方式,其本质是增加有效性校验。

本文只叙述在SpringBoot通过Druid的解决方式,其他方式请参考解决。

3.2  添加依赖

        <!-- MySqlDB -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

3.3  配置连接池属性

新增以下配置

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.filters=stat
spring.datasource.druid.max-active=20
spring.datasource.druid.initial-size=1
spring.datasource.druid.max-wait=60000
spring.datasource.druid.min-idle=1
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=select 1
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.test-on-return=true
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-open-prepared-statements=20

本文参考自:通过在SpringBoot中配置连接池属性解决MySql连接池Communications link failure问题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MySQL报错"The last packet successfully received from the server was X milliseconds ago"时,这通常是由于连接超时引起的。解决这个问题的方法有以下几种: 1. 检查网络连接:首先确保网络连接正常,可以尝试使用其他网络连接或者重启网络设备。 2. 调整连接超时时间:可以通过修改MySQL配置文件中的"wait_timeout"参数来增加连接超时时间。该参数表示MySQL服务器在没有活动连接的情况下等待的时间,默认为8小时。可以将其增加到更大的值,例如设置为28800(8小时)。 3. 优化查询性能:如果查询语句执行时间过长,可能会导致连接超时。可以通过优化查询语句、创建索引、调整数据库结构等方式来提高查询性能,减少连接超时的可能性。 4. 增加服务器资源:如果服务器资源不足,可能会导致连接超时。可以考虑增加服务器的内存、CPU等资源,以提高服务器的处理能力。 5. 使用长连接:长连接是指在一个连接上可以执行多个查询,而不是每次查询都建立一个新的连接。使用长连接可以减少连接建立和断开的开销,降低连接超时的可能性。 6. 检查防火墙设置:有时防火墙设置可能会导致连接超时。确保防火墙没有阻止MySQL服务器的访问。 希望以上方法能够帮助您解决MySQL报错"The last packet successfully received from the server was X milliseconds ago"的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明天争取

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值