zoukankan      html  css  js  c++  java
  • 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">
  • 相关阅读:
    Java应用程序的运行机制
    IO流——字符流
    IO流——字节流
    Java API --- File类
    SSM框架整合
    Mybatis核心组件
    AJAX 练习
    设计者模式
    软件七大设计原则
    并发编程
  • 原文地址:https://www.cnblogs.com/qin-derella/p/6128598.html
Copyright © 2011-2022 走看看