我需要为不同范围的查询设置查询超时,具体取决于它们提取的数据。也就是说,每个查询都有自己的超时时间。
例如,查询A-> 10分钟
查询B-> 5分钟
现在,我如何使用getJDBCTemplate()设置这些不同的时间。当我尝试下面的代码片段时,超时设置被忽略,并且无论设置如何,两个查询都在同一时间超时!
Thread t1 = new Thread(new Runnable() {
public void run() {
getJdbcTemplate().setQueryTimeout(5);
List t = getJdbcTemplate()
.query("select top 10000 * from ABC",new RowMapper<T>(){
..
});
Thread t2 = new Thread(new Runnable() {
public void run() {
getJdbcTemplate().setQueryTimeout(10);
List t = getJdbcTemplate()
.query("select top 30000 * from XYZ",new RowMapper<T>() {
..
});
t1.start();
t2.start();
Run Code Online (Sandbox Code Playgroud)
在上述情况下,两个查询都在第5分钟或第10分钟超时。有没有一种方法可以根据查询进行设置?请提出建议!
[更新]
<bean id="dSource" class="com.xyz.DSource" >
<property name="dataSource" ref="dataSource"/>
</bean>
public abstract class AbstractData {
private DSource dSource;
public JdbcTemplate getJdbcTemplate(){
ApplicationContext Ctx = ContextUtils.getApplicationContext();
dSource = (DSource)Ctx.getBean("dSource");
return dSource.getJDBCTemplate();
}
}
public class DSource extends JdbcDaoSupport{
public JdbcTemplate getJdbcTemplate(){
return getJdbcTemplate();
}
}
public Class Dao extends AbstractData{
public void callQuery(){
[AS already posted, t1 and t2 are 2 threads for 2 diff methods/queries using
getJDBCTemplate Of abstract classs]
Thread 1
Thread 2
}
}
Run Code Online (Sandbox Code Playgroud)
超时被覆盖可能是因为您的 JdbcTemplate 是单例的(请添加其配置)。为了实现你想要的,你需要为每个类(或方法)专用的 JdbcTemplate 。
Thread t1 = new Thread(new Runnable() {
public void run() {
JdbcTemplate template = new JdbcTemplate(dataSource);
template.setQueryTimeout(5);
List t = template
.query("select top 10000 * from ABC",new RowMapper<T>(){
..
});
Run Code Online (Sandbox Code Playgroud)
我不认为这是理想的。可能更好的解决方案是使用纯 jdbc 并将超时直接设置为 prpared 语句
Connection con = jdbcTemplate.getDataSource().getConnection()
preparedstatement = con.prepareStatement(sql);
preparedstatement.setQueryTimeout(theTimeout);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您不必检查 Spring 是否会关闭语句和连接,否则您需要自己处理。
| 归档时间: |
|
| 查看次数: |
16272 次 |
| 最近记录: |