mysql的“discard connection”&&“com.mysql.jdbc.exceptions.jdbc4.CommunicationsException”异常

本文详细解析了在使用MySQL连接池时遇到的discardconnection异常,深入分析了异常产生的原因,并提供了一种有效的解决方案,即调整连接池的testOnBorrow属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

服务器上有几个定时任务在跑,每分钟执行一次的那种,于是服务器上每隔一小会就会从报出"discard connection"的异常。

异常信息:

discard connection

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet successfully received from the server was 60,003 milliseconds ago. The last packet sent successfully to the server was 6 milliseconds ago. at sun.reflect.GeneratedConstructorAccessor141.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3562) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3462) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3905) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2495) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1903) at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1242) 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.wall.WallFilter.preparedStatement_execute(WallFilter.java:619) at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3049) 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.GeneratedMethodAccessor1981.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59) at com.sun.proxy.$Proxy324.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:326) at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109) at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:143) at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61) at com.sun.proxy.$Proxy317.query(Unknown Source) 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.GeneratedMethodAccessor167.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) at com.sun.proxy.$Proxy161.selectList(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230) at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:139) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:76) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59) at com.sun.proxy.$Proxy179.getTaskList(Unknown Source) at cn.ce.marketingcenter.cron.global.AppGeneralInfoTask.updateGeneralStatus(AppGeneralInfoTask.java:79) at cn.ce.marketingcenter.cron.global.AppGeneralInfoTask$$FastClassBySpringCGLIB$$f43c3b81.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88) at cn.ce.marketingcenter.config.aspect.cron.CronAspect.aspect(CronAspect.java:91) at sun.reflect.GeneratedMethodAccessor161.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:246) at org.springframework.cloud.context.scope.GenericScope$LockedScopedProxyFactoryBean.invoke(GenericScope.java:494) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) at cn.ce.marketingcenter.config.aspect.cron.CronAspect$$EnhancerBySpringCGLIB$$e4f729ac.aspect(<generated>) at sun.reflect.GeneratedMethodAccessor161.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633) at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88) at cn.ce.marketingcenter.config.aspect.cron.CronAspect.aspect(CronAspect.java:91) at sun.reflect.GeneratedMethodAccessor161.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633) at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) at cn.ce.marketingcenter.cron.global.AppGeneralInfoTask$$EnhancerBySpringCGLIB$$e4eed146.updateGeneralStatus(<generated>) at sun.reflect.GeneratedMethodAccessor299.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost. at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3014) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3472) ... 90 common frames omitted

 

 当时的druid配置:

druid:
  connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
  #池启动时创建的连接数量
  initialSize: 5
  #池中保持空闲的最少连接数
  minIdle: 5
  #最大连接数量
  maxActive: 2000
  #合并多个DruidDataSource的监控数据
  useGlobalDataSourceStat: true
  #获取连接超时等待时间,20秒
  maxWait: 20000
  # 通过别名的方式配置扩展插件,多个英文逗号分隔,常用的插件有:
  # 监控统计用的filter:stat
  # 日志用的filter:slf4j
  # 防御sql注入的filter:wall
  filters: stat,wall,slf4j
  #开启池的prepared statement 功能,并且指定每个连接上PSCache的大小
  poolPreparedStatements: true
  maxPoolPreparedStatementPerConnectionSize: 20
  #连接保持空闲而不被驱逐的最长时间,250秒(比mysql设定的wait_timeout小一点,提前关闭)
  minEvictableIdleTimeMillis: 250000
  #检查连接池中空闲连接的间隔,60秒
  timeBetweenEvictionRunsMillis: 60000
  #申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效
  testWhileIdle: true
  #表示从连接池中获取连接前是否运行validationQuery,true=运行[默认],false=不运行
  testOnBorrow: false
  #表示将连接归还连接池前是否运行validationQuery,true=运行,false=不运行[默认]
  testOnReturn: false
  validationQuery: SELECT 1 FROM DUAL

当时mysql的配置:

wait_timeout=300

 解决办法:

把testOnBorrow改为true,异常信息不再出现

原因分析:

因为在连接池拿到连接要去工作的时候,发现连接已经失效。不要因为配置了 testWhileIdle + minEvictableIdleTimeMillis 就认为能把testOnBorrow设置为false,还是要在从连接池拿连接的时候先检测一下有效性。

当出现`com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure`错误时,通常是由于连接MySQL数据库时出现了问题。这可能是由于以下原因之一导致的: 1. MySQL服务器未运行或已关闭。 2. MySQL服务器正在运行,但无法通过网络连接到它。 3. MySQL服务器正在运行,但由于某些原因(例如防火墙或安全设置),它拒绝了您的连接请求。 为了解决这个问题,您可以尝试以下几个步骤: 1. 确保MySQL服务器正在运行,并且您使用的是正确的主机名和端口号。 2. 检查您的网络连接,确保您可以通过网络连接到MySQL服务器。 3. 检查您的防火墙和安全设置,确保它们不会阻止您的连接请求。 4. 确保您使用的是正确的JDBC驱动程序版本,以便与您正在运行的MySQL服务器版本兼容。 以下是一个Java连接MySQL数据库的示例代码,可以帮助您更好地理解如何解决这个问题: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class MySQLConnection { public static void main(String[] args) { Connection conn = null; try { // 加载MySQL JDBC驱动程序 Class.forName("com.mysql.jdbc.Driver"); // 创建连接 String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; conn = DriverManager.getConnection(url, user, password); // 连接成功 System.out.println("Connected to the database."); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值