解决 ‘The last packet successfully received from the server was xxx milliseconds ago‘ 问题

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 解决 ‘The last packet successfully received from the server was xxx milliseconds ago‘ 问题

springboot + druid + mysql 在查询某些比较耗时的sql会报错

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet successfully received from the server was 10,010 milliseconds ago. The last packet sent successfully to the server was 10,010 milliseconds ago.
    at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
    at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:371)
    at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3446)
    at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:434)
    at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3444)
    at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:434)
    at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3444)
    at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:152)
    at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:483)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    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.$Proxy160.execute(Unknown Source)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:64)
    ... ...

查阅了一下,网上一般的做法有以下几种:

  1. 修改mysql的wait_timeout,修改spring数据源配置,增加maxWait(要比数据库wait_timeout小)、testOnBorrow=truequeryTimeout等配置,在datasource.url后面增加autoReconnect=true配置。

  2. 更新mysql-connector版本,我用的是8.0.28

  3. 更新jdk版本,如jdk1.7升级到jdk1.8

尝试过上面的几个方法都没办法解决。

从报错信息来看,这里设置的超时是10000毫秒,然后把mysql-connector源码下载下来debug了一下,发现有socketTimeout参数的值正好是10000,然后尝试在spring数据源配置上增加次配置依旧不行。

最后在datasource.url后面socketTimeout参数配置,重启测试发现能被正确识别,到此问题解决。

下面是druiddatasource的配置,仅供参考

druid:
  initial-size: 5
  min-idle: 2
  maxActive: 10
  maxWait: 60000
  timeBetweenEvictionRunsMillis: 60000
  minEvictableIdleTimeMillis: 300000
  validationQuery: SELECT 1 FROM DUAL
  testWhileIdle: true
  testOnBorrow: false
  testOnReturn: false
  poolPreparedStatements: true
  maxPoolPreparedStatementPerConnectionSize: 20
  filters: stat,slf4j
  connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000

datasource:
  driver-class-name: com.mysql.cj.jdbc.Driver
  url: jdbc:mysql://localhost:3306/xxx?useUnicode=true&useSSL=false&allowLoadLocalInfile=false&autoReconnect=true&failOverReadOnly=false&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8&connectTimeout=1000&socketTimeout=30000
  username: 
  password:
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2月前
|
监控
{"level":"warn","ts":"2023-11-07T00:35:53.400+0800","caller":"etcdserver/server.go:2048",&
{"level":"warn","ts":"2023-11-07T00:35:53.400+0800","caller":"etcdserver/server.go:2048",&
|
5月前
|
Ubuntu Linux C语言
Could not establish connection to “xx.xx.xx.xx“:The VS Code Server faild to start.【重要解决方案】
Could not establish connection to “xx.xx.xx.xx“:The VS Code Server faild to start.【重要解决方案】
156 0
|
网络安全 Docker 容器
failed to create network error response from daemon filed to setup ip tables问题
failed to create network error response from daemon filed to setup ip tables问题
204 0
|
Linux
七个办法只有一个有效:200 PORT command successful. Consider using PASV.425 Failed to establish connection.
七个办法只有一个有效:200 PORT command successful. Consider using PASV.425 Failed to establish connection.
617 0
七个办法只有一个有效:200 PORT command successful. Consider using PASV.425 Failed to establish connection.
|
NoSQL MongoDB
运行 mongo 出现 Error: couldn‘t connect to server 127.0.0.1:27017, connection attempt failed
运行 mongo 出现 Error: couldn‘t connect to server 127.0.0.1:27017, connection attempt failed
1014 0
运行 mongo 出现 Error: couldn‘t connect to server 127.0.0.1:27017, connection attempt failed
|
Java 关系型数据库 MySQL
问题记录:The last packet sent successfully to the server was ****
集成Mybatis的项目中出现了这个问题,初看很蒙比,最近一次成功发送请求到达服务器,你报错干什么?首先必须说的是这个一个关于Mybatis的报错,那什么原因导致了这个问题呢?
489 0
‘Client‘ is not allowed to run in parallel.Would you like to stop the running one?
‘Client‘ is not allowed to run in parallel.Would you like to stop the running one?
566 0
‘Client‘ is not allowed to run in parallel.Would you like to stop the running one?
|
SQL 数据库 Windows
SQL Server 2005 sp_send_dbmail出现Internal error at FormatRowset (Reason: Not enough storage is available to complete this operation)
案例环境:   操作系统: Windows 2003 SE 32bit(SP2) 数据库版本:Microsoft SQL Server 2005 - 9.00.5069.00 (Intel X86)             Aug 22 2012 16:01:52           ...
1422 0
|
Linux 网络架构
Root-NFS: Unable to get mountd port number from server, using default
问题描述:         以前下载到开发板linux内核启动好好地,今天突然启动不了了,到达Root-NFS: Unable to get mountd port number from server, using default这个位置就停住了,过了一段时间就显示,如图:       很明显,我的nfs有问题。
1462 0