mysql提示Statement cancelled due to timeout or client request
https://blog.csdn.net/qq_33919114/article/details/128739573
报错信息:
2025-01-22 17:33:19.707 ERROR [38586BF97FFEBFFEFBAE6DD96B7207BF96ED7] [XNIO-3 task-6] [Statement] [line:152] {conn-10012, pstmt-20190} execute error. UPDATE CIF_SEQUENCE SET INIT_VALUE = INIT_VALUE + INCREMENT_VALUE WHERE TABLE_NAME = ?
com.mysql.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2827)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2487)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1197)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3461)
at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3459)
at com.alibaba.druid.wall.WallFilter.preparedStatement_execute(WallFilter.java:626)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3459)
at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3459)
at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:167)
at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:497)
at sun.reflect.GeneratedMethodAccessor114.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.$Proxy249.execute(Unknown Source)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:47)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)
at sun.reflect.GeneratedMethodAccessor150.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
at com.sun.proxy.$Proxy247.update(Unknown Source)
at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.doUpdate(MybatisSimpleExecutor.java:54)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
at com.baomidou.mybatisplus.core.executor.MybatisCachingExecutor.update(MybatisCachingExecutor.java:83)
at sun.reflect.GeneratedMethodAccessor148.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
at com.sun.proxy.$Proxy246.update(Unknown Source)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197)
at sun.reflect.GeneratedMethodAccessor157.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:426)
at com.sun.proxy.$Proxy119.update(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:287)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:65)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:96)
at com.sun.proxy.$Proxy137.updateNextValue(Unknown Source)
at com.posp.abiz.srv.service.impl.SequenceServiceImpl.nextvalForAccount(SequenceServiceImpl.java:42)
at com.posp.abiz.srv.service.impl.SequenceServiceImpl$$FastClassBySpringCGLIB$$b90b91cd.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
at com.posp.abiz.srv.service.impl.SequenceServiceImpl$$EnhancerBySpringCGLIB$$40c41ec2.nextvalForAccount(<generated>)
at com.posp.abiz.srv.service.impl.AccountWithDrawDealServiceImpl.create(AccountWithDrawDealServiceImpl.java:45)Cause: com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request
简言:这种异常从字面翻译过来:mysql 请求链接超时,具体超时是什么原因导致的,可以根据情况分析下。
问题分析原因:当前业务上线已经有半年,突然监控到异常。分析原因,应该不是代码错误导致,不然问题早就被抛出来,那就是网络或是DB被占用资源请求队列阻塞原因导致请求超时。
通过云盘监控可以观察到数据库再这时间段有明显波动,找DBA确认有执行其它业务
具体操作:
altel table youdou_record convert to character set utf8m64_general_ci;
这语句意思是修改youdou_record 表字符集语句执行,会锁表,导致JDBC 连接数据库,执行inset 语句插入数据,抛出请求超时,同时数据库连接池堆栈信息也同步增长。
问题就分析到这了。
其他解决方式:
1.配置jdbc超时时间,适当增大,在JDBC连接参数里加上queryTimeout=2400即可解决。
2.加索引,优化SQL,给关联字段增加索引
3.子查询尽量缩小搜索范围,减少join的次数
4.能用join就不要in了
5.限制索引的数目
6.不要查询*(所有),避免大字段,查询需要信息即可
其他情况:nacos启动不起来
问题:nacos 启动报错,启动失败,全部报错下面贴出。
先说结论:排查结果为服务器带宽不够,mysql 查询的数量量太大,传输时间损坏在网络io上!
事故起因:
前期config_info 表数据量少的时候,并没有出现问题,当nacos 的配置中心的配置文件在逐渐增多之后,nacos 从偶尔启动失败,到一直启动失败。下图为错误堆栈信息:
排查经过:
a. 开始进行了搜索, 一些博客指出在jdbc 连接加上queryTimeout=2400 试了之后并没有用
b.排查业务逻辑发现sql 查询时间是4s 多。现在思路就是要不加大 限制时间(queryTimeout 并没有用,也没找到..) 要不mysql调优将sql 查询时间降下来。
尝试:
由于服务器的mysql ,和nacos 在相同地方,接下来,我又将 地址换为localhost 进行更换,nacos启动成功 ,暂时先用着。换为ip 地址就启动失败

最终确定问题:
服务器带宽不够,这条sql 查询出来的数据量比较大,从本地查询出来,浪费在网络传输,5m 传的慢。 阿里客服那边有不同带宽可以测试,10 m 是两秒 100m 是0. 2s 意思