Mysql 异常,“Cause: com.mysql.cj.jdbc.exceptions.MySQLTimeoutException”

Cause: com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request

简言:这种异常从字面翻译过来:mysql 请求链接超时,具体超时是什么原因导致的,可以根据情况分析下。

异常详情

org.springframework.dao.QueryTimeoutException: 
### Error updating database.  Cause: com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request
### The error may exist in file [/app/resources/mybatis/YouDouRecordMapper.xml]
### The error may involve com.txxy.activity.mapper.YouDouRecordMapper.insert-Inline
### The error occurred while setting parameters
### SQL: insert into youdou_record(                   id, u_id, change_Amount, source_id, source_name, type, create_time, create_user, modify_time, modify_user               ) values(null, ?, ?, ?, ?, ?, now(), ?, now(), ?)
### Cause: com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request
; Statement cancelled due to timeout or client request; nested exception is com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request
	at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:76) ~[spring-jdbc-5.3.22.jar:5.3.22]
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70) ~[spring-jdbc-5.3.22.jar:5.3.22]
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:79) ~[spring-jdbc-5.3.22.jar:5.3.22]
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:88) ~[mybatis-spring-2.0.5.jar:2.0.5]
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440) ~[mybatis-spring-2.0.5.jar:2.0.5]

问题分析原因:当前业务上线已经有半年,突然监控到异常。分析原因,应该不是代码错误导致,不然问题早就被抛出来,那就是网络或是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.不要查询*(所有),避免大字段,查询需要信息即可

以上资料仅供参考,欢迎大家留言讨论~ ღ( ´・ᴗ・` )比心

扫描二维码关注公众号,回复: 16709766 查看本文章

猜你喜欢

转载自blog.csdn.net/weixin_43829047/article/details/130943378