mybatis配置sql超时时间

mybatis如果不配置,默认超时时间是不做限制的。当系统慢sql很多时,势必会增加数据库压力,系统性能及稳定性降低。所以有必要要设置sql超时设置,下面配置超时时间是5分钟。

 

第一步:全局配置如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD  Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 全局超时配置,300表示sql执行时间超过5分钟时,报错 -->
<configuration>
    <settings>
        <setting name="defaultStatementTimeout" value="300" />
    </settings>
</configuration>

 第二步:在sqlSessionFactory引入该配置

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mapperLocations" value="classpath*:config/mybatis/**/mapper_*.xml" />
    <!-- 引入mysql的全局配置,超时,缓存等 -->
    <property name="configLocation" value="classpath:config/mybatis/mysql.xml" />
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

上面的配置,后面所有通过 sqlSessionTemplate的查询都是有超时限制的。如果时间超过5分钟就会报错。报错信息为Statement cancelled due to timeout or client request。

 

 

当然对于个别情况,有的sql需要执行很长时间或其他的话,可以对单个sql做个性化超时设置。

在mapper xml文件中对具体一个sql进行设置,方法为在select/update/insert节点中配置timeout属性,依然是以秒为单位表示超时时间并只作用于这一个sql

<select id="queryList" parameterType="hashmap" timeout="10000">

 

转载于:https://www.cnblogs.com/qin-derella/p/6128598.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值