有没有办法为MyBatis配置设置可定制的超时时间?
我在Spring框架中使用MyBatis,但我不能将'defaultStatementTimeout‘属性设置为可自定义,就像Spring中的PropertyPlaceHolder一样。
发布于 2013-06-22 07:21:07
有一种方法,但只能通过MyBatis配置文件。您可以在您的MyBatis配置文件( MyBatis页面中有一个example )中添加Spring配置文件的位置,以加载您想要的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>发布于 2013-06-28 06:16:25
我已经解决了我的问题。我必须用CustomSqlSessionFactoryBean.覆盖SqlSessionFactoryBean
<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()方法(将MyBatis配置文件中的${custom.timeout}替换为超时值)。
@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方法。
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}变量自定义超时。
发布于 2015-07-09 16:48:04
对于Spring Batch,可以使用bean上的" timeout“属性在SystemCommandTasklet上设置自定义超时
<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“属性覆盖语句超时:
<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>https://stackoverflow.com/questions/17245071
复制相似问题