首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MyBatis上的可自定义超时

MyBatis上的可自定义超时
EN

Stack Overflow用户
提问于 2013-06-22 06:08:54
回答 3查看 13.7K关注 0票数 2

有没有办法为MyBatis配置设置可定制的超时时间?

我在Spring框架中使用MyBatis,但我不能将'defaultStatementTimeout‘属性设置为可自定义,就像Spring中的PropertyPlaceHolder一样。

EN

回答 3

Stack Overflow用户

发布于 2013-06-22 07:21:07

有一种方法,但只能通过MyBatis配置文件。您可以在您的MyBatis配置文件( MyBatis页面中有一个example )中添加Spring配置文件的位置,以加载您想要的settings

代码语言:javascript
复制
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="location" value="classpath:mybatis-config.xml" />
</bean>

MyBatis配置文件可以是:

代码语言:javascript
复制
<?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">
<configuration>
    <settings>
        <setting name="defaultStatementTimeout" value="10"/> <!-- seconds -->
    </settings>
</configuration>
票数 3
EN

Stack Overflow用户

发布于 2013-06-28 06:16:25

我已经解决了我的问题。我必须用CustomSqlSessionFactoryBean.覆盖SqlSessionFactoryBean

代码语言:javascript
复制
<bean id="sqlSessionFactory" class="com.myapp.CustomSqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:sql-config.xml" />
    <property name="timeout" value="${custom.timeout}" />
</bean>

我的MyBatis配置文件如下所示

代码语言:javascript
复制
<configuration>
    <settings>
        <setting name="defaultStatementTimeout" value="${custom.timeout}"/>
    </settings>
</configuration>

我的SqlSessionFactoryBean自定义实现覆盖了设置超时的buildSqlSessionFactory()方法(将MyBatis配置文件中的${custom.timeout}替换为超时值)。

代码语言:javascript
复制
@Override
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
    String strConfig = IOUtils.toString(CustomSqlSessionFactoryBean.class.getResourceAsStream(MYBATIS_CONFIG_PATH));
    strConfig = StringUtils.replace(strConfig, TO_REPLACE, String.valueOf(timeout));
    InputStream inputStream = IOUtils.toInputStream(strConfig);
    setConfigLocation(new CustomClasspathResource(inputStream, strConfig));
    return super.buildSqlSessionFactory();
}

这个类让我将修改后的inputstream设置为setConfigLocation方法。

代码语言:javascript
复制
class CustomClasspathResource extends ClassPathResource {

    private InputStream inputStream;

    @Override
    public InputStream getInputStream() throws IOException {
        return inputStream;
    }

    private CustomClasspathResource(InputStream inputStream, String path) {
        super(path);
        this.inputStream = inputStream;
    }

因此,可以使用${custom.timeout}变量自定义超时。

票数 1
EN

Stack Overflow用户

发布于 2015-07-09 16:48:04

对于Spring Batch,可以使用bean上的" timeout“属性在SystemCommandTasklet上设置自定义超时

代码语言:javascript
复制
<bean id="FlxxtUpdaterTasklet" class="org.springframework.batch.core.step.tasklet.SystemCommandTasklet">
    <property name="command" value="xxx" />
    <property name="timeout" value="3600000" />
    <property name="interruptOnCancel" value="true" />
    <property name="terminationCheckInterval" value="5000" />
</bean>

在Mybatis中使用mapper语句上的" Timeout“属性覆盖语句超时:

代码语言:javascript
复制
    <mapper namespace="com.xxxx.blsf.batch.mapperbls.Bls2CsvMapper">
        <select id="bls2csv" parameterType="com.xxxx.blsf.batch.mapperparams.SpParamInteger" 
            statementType="CALLABLE" timeout="6000" >
            { CALL PKG_BLACKLIST_PLUS_RC.BLS_2_CSV(#{rcInt, mode=OUT, jdbcType=INTEGER, javaType=java.lang.Integer})}
        </select>
    </mapper>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17245071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档