当前位置: 首页 > news >正文

使用mysql报Communications link failure异常解决

背景

线上使用polarDB,基于mysql(5.7),架构为springboot+mybatisplus+durid连接池,部分业务场景涉及大表更新和查询操作,在查询慢sql且超过一定时间时就会报出"Communications link failure"异常,主要体现在界面查询或定时任务处理大批量数据是执行数据库的更新、查询异常。

现象

不区分业务高峰期,只要慢sql超过一定时间(本例为10秒)无论如何更新设置连接池参数和polarDB实例参数配置,异常依旧存在。

异常如下:

### Cause: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failureThe last packet successfully received from the server was 10,011 milliseconds ago. The last packet sent successfully to the server was 10,011 milliseconds ago.
; Communications link failureThe last packet successfully received from the server was 10,011 milliseconds ago. The last packet sent successfully to the server was 10,011 milliseconds ago.; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failureThe last packet successfully received from the server was 10,011 milliseconds ago. The last packet sent successfully to the server was 10,011 milliseconds ago.at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:100)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:91)at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)at com.sun.proxy.$Proxy152.selectList(Unknown Source)at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224)at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForIPage(MybatisMapperMethod.java:121)at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:85)at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148)at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)at com.sun.proxy.$Proxy535.selectPage(Unknown Source)at sun.reflect.GeneratedMethodAccessor2431.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

解决方案记录

根据ChatGPT或网上搜索的解决方案,对连接池大小、时间参数和连接池回收配置等都进行了优化配置,如max-wait、connect-timeout等超时配置,如下:

#mysql配置
spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
#spring.datasource.druid.driverClassName = net.sf.log4jdbc.sql.jdbcapi.DriverSpy
spring.datasource.druid.driverClassName = com.mysql.cj.jdbc.Driver
#mysql 8.x使用 spring.datasource.druid.url
spring.datasource.druid.url = jdbc:mysql://xxx:3306/xxx?serverTimezone=GMT%2B8&characterEncoding=utf8&autoReconnect=true
spring.datasource.druid.username = xxx
spring.datasource.druid.password = xxx
spring.datasource.druid.initial-size = 5
spring.datasource.druid.min-idle = 10
spring.datasource.druid.max-active = 500
spring.datasource.druid.max-wait = 30000
spring.datasource.druid.connect-timeout = 30000
spring.datasource.druid.query-timeout = 30000
spring.datasource.druid.transaction-query-timeout = 30000
spring.datasource.druid.time-between-eviction-runs-millis = 60000
spring.datasource.druid.min-evictable-idle-time-millis = 300000
spring.datasource.druid.max-evictable-idle-time-millis = 900000
spring.datasource.druid.test-while-idle = true
spring.datasource.druid.test-on-borrow = false
spring.datasource.druid.test-on-return = false
spring.datasource.druid.validation-query = select 1
spring.datasource.druid.webStatFilter.enabled = true
spring.datasource.druid.stat-view-servlet.enabled = true
spring.datasource.druid.stat-view-servlet.url-pattern = /druid/*
spring.datasource.druid.stat-view-servlet.reset-enable = false
spring.datasource.druid.filter.stat.enabled = true
spring.datasource.druid.filter.stat.log-slow-sql = true
spring.datasource.druid.filter.stat.slow-sql-millis = 1000
spring.datasource.druid.filter.stat.merge-sql = true
spring.datasource.druid.filter.wall.config.multi-statement-allow = true

polarDB/mysql实例配置 connect_timeout参数超过10秒;

问题得不到解决,业务层对索引优化和强制索引等都进行了处理,部分数据迁移到ES/IOTDB,但根本问题在于MYSQL中依旧存在不可避免的大表慢sql,导致该问题依旧存在。

最后在druid的github issue中找到了高度相似问题,解决方案如下:

连接配置url中增加socketTimeout参数如下,

spring.datasource.druid.url = jdbc:mysql://xxx:3306/xxx?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&autoReconnect=true&socketTimeout=30000

重启服务解决问题。

注意,该方式仅解决类似"The last packet successfully received from the server was 10,011 milliseconds ago. The last packet sent successfully to the server was 10,011 milliseconds ago."的异常提示问题。

根据堆栈或连接池druid DruidDataSource溯源源码,使用spring.datasource.druid.socket-timeout配置存在参数覆盖问题,即使使用了connect-timeout配置也无法解决该异常。

至于为什么部分查询未走索引导致慢sql导致超时问题,和索引顺序结构、查询条件、执行优化器等有很大关系,编码优化、索引优化或使用升级修复执行器索引的db版本,也是开发人员需要评估和优化的点。

http://www.dtcms.com/a/618128.html

相关文章:

  • 【Linux驱动开发】Linux USB驱动架构详解
  • Linux服务器配置ssh免密登陆多台服务器、服务器别名配置
  • 百度推广青岛公司网站在线优化
  • java学习3-redis集成
  • 【Linux】深入理解进程(四)(进程地址空间)
  • 数据结构1.0 面试题
  • 网站定制哪家快高档网站设计公司
  • 信创产品认证机构有哪些?信创检测报告和信创产品评估证书如何办理?
  • 官方网站建设计划书文山做网站yunling88
  • 数据分析笔记12:函数
  • 如何驯服AI编程
  • 电动自行车起火酿事故,智慧社区如何分阶段阻绝灾害发生?
  • 淄博做网站的公司排名优化系统
  • 基于Rust构建一个实用的图片转Base64工具
  • asp.net mvc做网站难吗胶州网站优化
  • [C#] NO.4 我的第一个项目
  • 运放之电压跟随器
  • CSS3 框大小
  • 商城网站系统资源
  • 网站源码怎么有苏州首页关键词优化
  • 深度学习实战(基于pytroch)系列(二十二)多通道输入输出
  • 本周热搜(11月9日~11月16日)
  • React useMemo(当依赖项未变化,重复渲染时直接返回上一次缓存计算结果,而非重新执行计算)
  • 【IOS开发】Objective-C 与 Swift 的对比
  • 在STM32 HAL库中使用 WFI 实现低功耗准确延时
  • 编程语言落地手册:erlang实现与汇编剖析
  • 搜索下单(es、mysql、MQ同步;关于事务失效)
  • aleph-node Node upgrade instructions 节点升级说明
  • 找谁做网站网站建设与运营培训班
  • 智能制造与工业4.0:5G与物联网的深度融合