java MyBatis 上可自定义的超时时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17245071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Customizable timeout on MyBatis
提问by rvillablanca
Is there any way to do a customizable timeout for MyBatis configuration?
有没有办法为 MyBatis 配置做一个可自定义的超时?
I am using MyBatis with Spring framework, but I cannot make the 'defaultStatementTimeout' property customizable, as PropertyPlaceHolder on Spring.
我正在将 MyBatis 与 Spring 框架一起使用,但我无法像 Spring 上的 PropertyPlaceHolder 那样使“defaultStatementTimeout”属性可定制。
回答by Paul Vargas
There is a way, but only through of the MyBatis configuration file. You can add the location of MyBatis configuration file in your Spring configuration file (there is a examplein the MyBatis page) for load the settingsthat you want:
有一种方法,但只能通过MyBatis 的配置文件。您可以在 Spring 配置文件中添加 MyBatis 配置文件的位置(MyBatis 页面中有一个示例)以加载您想要的设置:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="location" value="classpath:mybatis-config.xml" />
</bean>
The MyBatis configuration file can be:
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>
回答by rvillablanca
I have solved my problem. I had to override the SqlSessionFactoryBean
with a CustomSqlSessionFactoryBean.
我已经解决了我的问题。我不得不SqlSessionFactoryBean
用一个覆盖CustomSqlSessionFactoryBean.
<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>
My MyBatis config file looks like
我的 MyBatis 配置文件看起来像
<configuration>
<settings>
<setting name="defaultStatementTimeout" value="${custom.timeout}"/>
</settings>
</configuration>
My Custom implementation of SqlSessionFactoryBean
overwrite the buildSqlSessionFactory()
method, which set the timeout (replace ${custom.timeout} by timeout value in MyBatis config file).
我的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();
}
This class let me set the modified inputstream to the setConfigLocation
method.
这个类让我将修改后的输入流设置为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;
}
Thus the timeout is customizable with the ${custom.timeout}
variable.
因此,超时可以使用${custom.timeout}
变量进行自定义。
回答by Marco S.
For Spring Batch it's possible set custom timeout on SystemCommandTasklet using the property "timeout" on bean
对于 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>
With Mybatis for override the Statement Timeout using the property "timeout" on the mapper statement:
使用 Mybatis 使用映射器语句上的属性“超时”覆盖语句超时:
<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>