2013-06-21 14 views
1

有沒有辦法爲MyBatis配置進行可定製的超時?MyBatis上的可定製超時

我在Spring框架中使用MyBatis,但是我無法將'defaultStatementTimeout'屬性作爲Spring的PropertyPlaceHolder進行定製。

回答

1

有一種方法,但只能通過MyBatis配置文件。您可以在Spring配置文件中添加的MyBatis配置文件的位置(也就是在MyBatis的頁面example)爲負載settings要:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="location" value="classpath:mybatis-config.xml" /> 
</bean> 

MyBatis的配置文件可以是:

<?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> 
+0

嗨,謝謝你的回答。但是,這不是可定製的。 – rvillablanca

0

我已經解決了我的問題。我不得不重寫SqlSessionFactoryBeanCustomSqlSessionFactoryBean.

<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配置文件看起來像

<configuration> 
    <settings> 
     <setting name="defaultStatementTimeout" value="${custom.timeout}"/> 
    </settings> 
</configuration> 

我的SqlSessionFactoryBean自定義實現覆蓋buildSqlSessionFactory()方法,即設置超時(替換$ {custom.timeout }通過MyBatis配置文件中的超時值)。

@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(); 
} 

這個類讓我將修改的輸入流設置爲setConfigLocation方法。

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}變量進行自定義。

0

對於Spring Batch的它是在映射語句中使用屬性「超時」使用屬性「超時」的豆

<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的覆蓋聲明超時上SystemCommandTasklet可能一套定製超時:

<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>