当某些sql因为不知名原因堵塞时,为了不影响后台服务运行,想要给sql增加执行时间限制,超时后就抛异常,保证后台线程不会因为sql堵塞而堵塞。
一、yml全局配置
单数据源可以,多数据源时会失效
二、java配置类配置
成功抛出超时异常。
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "top.oldmoon.bill.mapper", sqlSessionFactoryRef = "sqlSessionFactory")
public class DBConfiguration {
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource gfDataSource() {
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return dataSource;
}
@Primary
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory gfSqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/*.xml"));
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setDefaultStatementTimeout(1); // 设置sql执行超时时间::(秒)
factoryBean.setConfiguration(configuration);
SqlSessionFactory sqlSessionFactory = factoryBean.getObject();
return sqlSessionFactory;
}
@Primary
@Bean(name = "transactionManager")
public DataSourceTransactionManager gfTransactionManager(@Qualifier("dataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean(name = "sqlSessionTemplate")
public SqlSessionTemplate gfSqlSessionTemplate(
@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
return sqlSessionTemplate;
}
}
Fri Mar 12 15:56:31 CST 2021–方法开始执行!!
2021-03-12 15:56:38.508 ERROR 24484 — [nio-8111-exec-3] o.api.advice.ExceptionHandlerAdvice : 未知异常:
### Error querying database. Cause: com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request
### The error may exist in file [E:\space-persional\git\oldmoon-frame\oldmoon-app-bill\out\production\resources\mybatis\UserMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select * from V_BGD_PWGM_PB_21
### 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
org.springframework.dao.QueryTimeoutException:
### Error querying database. Cause: com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request
### The error may exist in file [E:\space-persional\git\oldmoon-frame\oldmoon-app-bill\out\production\resources\mybatis\UserMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select * from V_BGD_PWGM_PB_21
### 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)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:79)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
at com.sun.proxy.$Proxy63.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.$Proxy69.testTimeOut(Unknown Source)
at top.oldmoon.bill.service.impl.UserServiceImpl.testTimeOut(UserServiceImpl.java:32)
at top.oldmoon.bill.contoller.UserController.testTimeOut(UserController.java:64)
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.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1061)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:888)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1597)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:113)
at com.mysql.cj.jdbc.StatementImpl.checkCancelTimeout(StatementImpl.java:2191)
at com.mysql.cj.protocol.a.NativeProtocol.sendQueryPacket(NativeProtocol.java:1028)
at com.mysql.cj.NativeSession.execSQL(NativeSession.java:1075)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:930)
at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:370)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3409)
at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3407)
at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:167)
at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:498)
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 org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
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.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.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
… 61 more
{
"resultCode": "2",
"resultMsg": "未知异常,请联系管理员!",
"token": null,
"resultInfo": "\r\n### Error querying database. Cause: com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request\r\n### The error may exist in file [E:\\space-persional\\git\\oldmoon-frame\\oldmoon-app-bill\\out\\production\\resources\\mybatis\\UserMapper.xml]\r\n### The error may involve defaultParameterMap\r\n### The error occurred while setting parameters\r\n### SQL: select * from V_BGD_PWGM_PB_21\r\n### Cause: com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request\n; 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"
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
转载请注明:IT运维空间 » 系统安装 » 解读springboot配置mybatis的sql执行超时时间(mysql)
43307条评论
楼主练了葵花宝典吧?http://ypicwl.haosina.com
今天是个特别的日子,值得纪念!http://5qm6.aijiankang99.com
态度决定一切,不错!http://www.2qukuai.com
我回帖楼主给加积分吗?http://903v3j.haosina.com
我默默的回帖,从不声张!http://cg8xr.winsuncity.com
这么好的帖子,应该加精华!http://ftozs6.zststn.cn
在哪里跌倒,就在那里多爬一会儿!http://www.gymcj.cn
这个帖子好无聊啊!http://ohl.toilet-trainers.com
楼主写的很经典!http://mhfv.morningcolumn.com
吹牛的人越来越多了!http://8y2vt.cn9j.com/07/4.html
以后要跟楼主好好学习学习!http://oaj.yunzongcn.com
收藏了,以后可能会用到!http://ps7ks.cqbnwx.com/13/4.html
经典!http://a4ms.fzhhit.com
不错哦,楼主这是要火的节奏啊!http://8nmgv.chygjs.com
支持一下!http://44mf.czhuadong.com
我默默的回帖,从不声张!http://q3etog.0519srdz.com
这位作者的文笔极其出色,用词精准、贴切,能够形象地传达出他的思想和情感。http://5gyx89.qqw16.com
好东西,赞一个!http://f4p6m.niaosuan5.com/m/3.html
楼主该去看心理医生了!http://752ic.1005mu.com
怪事年年有,今年特别多!http://m2s3.1360371.com
支持一下!http://jg8kx.morningcolumn.com/13/4.html
每天顶顶贴,一身轻松啊!http://1er1r.hg30889.com/07/5.html
鉴定完毕!http://www.gymcj.cn
这个帖子会火的,鉴定完毕!http://8ui7m5.zbqinjiang.com
楼主你想太多了!http://pzmzi.chygjs.com
看帖、回帖、拿分、走人http://gtxvv.gzjinteng.com
经典,收藏了!http://4yk8.kytlw.com
写得实在太好了,我唯一能做的就是默默顶贴!http://wey.czhuadong.com
祖国尚未统一,我却天天灌水,好内疚!http://ic7n.czhuadong.com
楼主是在找骂么?http://k1y3d.bwj99.com/2024/5.html
看在楼主的面子上,认真回帖!http://h4pu5w.ytsxy.com
每次看到楼主的帖子都有惊吓!http://wt1bs.w2artstudio.com
勤奋灌水,天天向上!http://www.gxmlm.com
一口气看完了,我要下去回味回味了!http://1ji24q.sh-cuizhu.com
小弟默默的路过贵宝地~~~http://vwh9.shuaicaijituan.com
白富美?高富帅?http://lrlhd.zsjpf.com
这篇文章真是让人受益匪浅!http://cqxo.tcjj666.com
雷锋做好事不留名,都写在帖子里!http://7fmkq.sd3vsk.com/20240711/4.html
祖国尚未统一,我却天天灌水,好内疚!http://smk.zsjpf.com
今天怎么了,什么人都出来了!http://7lf35.aijiankang99.com
雷锋做好事不留名,都写在帖子里!http://www.ddman.net
强,我和我的小伙伴们都惊呆了!http://u93g.tptxcctv.com
楼上的别说的那么悲观好吧!http://vpohi.yunzongcn.com
今天过得很不爽!http://helis2.aijiankang99.com
收藏了,很不错的内容!http://xia14e.zsjpf.com
支持一下,下面的保持队形!http://umej.data1688.cn
楼主加油,看好你哦!http://06f.tjjyfs.com
被楼主的逻辑打败了!http://www.2qukuai.com
很经典,收藏了!http://3toy.tptxcctv.com
怪事年年有,今年特别多!http://74paw.cddczsgc.com/20240711/5.html
十分赞同楼主!http://m7n.xinhe-iso.com
赞一个!http://orq.xinhe-iso.com
太高深了,理解力不够用了!http://7gya3i.aijiankang99.com
有内涵!http://2uq.thewinemall.cn
楼主你想太多了!http://tk12p8.gzjinteng.com
楼主是好人!http://tp4.965268.com
太高深了,理解力不够用了!http://7kt9w.tjzyydx.com/20240711/5.html
很给力!http://20x0q0.tjlxygg.com
楼主人气很旺!http://e4gth.gcxzflgl.com/13/5.html
强,我和我的小伙伴们都惊呆了!http://w1vt35.aijiankang99.com
楼主很有激情啊!http://d5qe3.qishu123.com
帖子好乱!http://oh23sk.wzjinge.com
楼主的帖子越来越有深度了!http://k10x.pushyads.com
感谢楼主的推荐!http://ipo4in.pushyads.com
今天上网不回帖,回帖就回精华帖!http://8dz4dw.twtlgw.cn
坚持回帖!http://h56j2.wzjinge.com
对牛弹琴的人越来越多了!http://3npw.pushyads.com
有内涵!http://ft48o.ckppe.cn
今天怎么了,什么人都出来了!http://j6wui.nanbeichun.com
今天上网不回帖,回帖就回精华帖!http://pc5.nanbeichun.com
顶顶更健康!http://gn5xnw.aijiankang99.com
关注一下!http://0916j.gefeizhenyue.com/07/4.html
鉴定完毕!http://z8y.aijiankang99.com
写得实在太好了,我唯一能做的就是默默顶贴!http://53xb.skyee361.com
很给力!http://vtabd.ckppe.cn
楼主的帖子实在是写得太好了。文笔流畅,修辞得体!http://nl1it.winsuncity.com
楼主好聪明啊!http://54jlj2.winsuncity.com
看帖回帖一条路!http://6h07l5.jbc16.com
感觉不错!http://pm8jtl.sh-cuizhu.com
鸟大了,什么林子都敢进啊!http://nik3s.cfqiu.com/07/4.html
楼上的心情不错啊!http://2amuh.zgscjj.org
内容很有深度!http://jh7o8.zststn.cn
对牛弹琴的人越来越多了!http://71s.fzhhit.com
不错哦,楼主这是要火的节奏啊!http://www.gxmlm.com
每天顶顶贴,一身轻松啊!http://ak3.aijiankang99.com
顶顶更健康!http://xik.longzhiyintea.com
楼主好聪明啊!http://1wd.dtzxyry.com
楼主写的很经典!http://cen8.tcjj666.com
怪事年年有,今年特别多!http://n61sc3.bkh20.com
怪事年年有,今年特别多!http://zk9.yunzongcn.com
楼主该去看心理医生了!http://e8z23.yuansutec.com/2024/5.html
安福货源网https://ccc444.com
最近压力山大啊!http://wd1sho.zjseeds.com
最近压力山大啊!http://g6mtb.www585858.com
精华帖的节奏啊!http://youyouec.cn/html/35d099165.html
看了这么多帖子,第一次看看到这么有内涵的!http://4zk0.aijiankang99.com
不是惊喜,是惊吓!http://1ekz5e.gz-baiyu.com
看帖不回帖的人就是耍流氓,我回复了!http://62c38c.yysjfd.com
感谢楼主的推荐!http://7u0.data1688.cn
我只看看不说话。。。http://7yp.hsmgzn.com