首頁 >Java >java教程 >springboot配置mybatis的sql執行逾時時間怎麼解決

springboot配置mybatis的sql執行逾時時間怎麼解決

WBOY
WBOY轉載
2023-05-15 18:10:252993瀏覽

當某些sql因為不知名原因堵塞時,為了不影響後台服務運行,想要給sql增加執行時間限制,超時後就拋異常,保證後台線程不會因為sql堵塞而堵塞。

一、yml全域配置

單一資料來源可以,多重資料來源時會失效

springboot配置mybatis的sql執行逾時時間怎麼解決

二、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      : 未知例外:

查詢資料庫時發生錯誤。原因: com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: 由於逾時或客戶端請求而取消語句

檔案中可能存在錯誤 [E:\space-persional\git\oldmoon-frame\oldmoon- app-bill\out\product\resources\mybatis\UserMapper.xml]

錯誤可能涉及defaultParameterMap
##設定參數時發生錯誤

# SQL: select * from V_BGD_PWGM_PB_21

# SQL: select * from V_BGD_PWGM_PB_21

原因:com.mysql.cj.jdbc.exceptions.MySQLTimeoutException:因為逾時或客戶端請求而取消語句
;因逾時或客戶要求而取消對帳單;巢狀異常是 com.mysql.cj.jdbc.exceptions.MySQLTimeoutException:由於逾時或客戶端請求而取消語句
org.springframework.dao.QueryTimeoutException:

# 查詢資料庫時發生錯誤。原因: com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: 由於逾時或客戶端請求而取消語句

檔案中可能存在錯誤 [E:\space-persional\git\oldmoon-frame\oldmoon- app-bill\out\product\resources\mybatis\UserMapper.xml]

錯誤可能涉及defaultParameterMap
##設定參數時發生錯誤

# SQL: select * from V_BGD_PWGM_PB_21

# SQL: select * from V_BGD_PWGM_PB_21

原因:com.mysql.cj.jdbc.exceptions.MySQLTimeoutException:因為逾時或客戶端請求而取消語句
;因逾時或客戶要求而取消對帳單;巢狀異常是com.mysql.cj.jdbc.exceptions.MySQLTimeoutException:由於逾時或客戶端請求而取消語句
   位於org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSjdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclasslator.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclasslator.SQLExceptionSubclass).位於org .springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70)
   位於org.springframework.jdbc.Pic. .mybatis.spring .MyBatisExceptionTranslator .translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
    在org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SjavairionTemplate.com (未知來源)
    在org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)
    在org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:1399 .ibatis .binding.MapperMethod.execute(MapperMethod.java:76)
   位於org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
    位於com.sun.proxy.$Proxy.java:59)
    位於com.sun。 testTimeOut(來源未知)
    位於top.oldmoon.bill.service.impl.UserServiceImpl.testTimeOut(UserServiceImpl.java:32)
    位於top.oldmoon.bill.contoller.UserController.testTimeOut(UserController.java:64 Im :43)
    在java.lang.reflect.Method.invoke(Method.java:498)
    在org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)##d在org. springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141)
    在org.springframework.web.servlet.mvc.method.annotation.Servlet. ##    在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894)
   位於org.springamework.webdler. .java:第808章###    在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:961)###    在org.springframework.web.servlet.FrameworkServlet. ###    在org. springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)###在 javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
    在 org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
  . .service(HttpServlet.java:733)
   位於org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
   位於org.apache.catalFil.Chain.java:231)
 :166)
   位於org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
   位於org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Application193). ##    在org.apache.catalina。 .apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
   位於org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)##. .RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)##> .java:193)
   位於org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
   位於org.springframework.web.Filter.FormentFilter.doFilterInterjava. ##    在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    在org.apache.catalina.core.PerRequestFilterChain.internalDoFilFilter(1999S.catal. .catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
   位於org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) 1. .doFilter(OncePerRequestFilter.java:119)
   位於org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  位於org.apache.catalina. :166)
    在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    在org.apache.catalina.core.StandardContextValve.invoke(ContextValve.Standard:在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
    在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)#appache. .valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
   位於org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
   位於org.apache.catalina.connector. (CoyoteAdapter.java:343)
   在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
   在org.apache.coyote.AbstractProcessorLight.com ##    在org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:888)
    在org.apache.tomcat.util.net.NioEndpoint$Socketpointor.doRun(NioEndpoint.doRun(157.NioEndpoint$Socketpointor.doRun(NioEndpoint.)在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
   在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)##o. .ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
   位於org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)## . (Thread.java:748)
引起:com.mysql.cj.jdbc.exceptions.MySQLTimeoutException:由於逾時或客戶端請求而取消語句
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapMapMappingtransException (SQLExceptionsMapping.java:113)
   位於com.mysql.cj.jdbc.StatementImpl.checkCancelTimeout(StatementImpl.java:2191)
   位於com.mysql.cj.protocol.a.NativeeryProto. :1028)
   在com.mysql.cj.NativeSession.execSQL(NativeSession.java:1075)
   在com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPparedd)    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"
}

以上是springboot配置mybatis的sql執行逾時時間怎麼解決的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除