gpt4 book ai didi

spring - JDBCTemplate setQueryTimeout 特定于每个查询,即查询级别

转载 作者:行者123 更新时间:2023-12-04 20:44:08 31 4
gpt4 key购买 nike

我需要根据他们提取的数据为各种查询范围设置查询超时。即,每个查询都有自己的超时时间。

比如说,查询 A - > 10 MINUTES
查询 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();

在上述上下文中,两个查询都在第 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
}

}

最佳答案

超时被覆盖可能是因为您的 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>(){
..
});

我不认为这是理想的。可能更好的解决方案是使用纯 jdbc 并将超时直接设置为 prpared 语句
    Connection con = jdbcTemplate.getDataSource().getConnection()
preparedstatement = con.prepareStatement(sql);
preparedstatement.setQueryTimeout(theTimeout);

在这种情况下,您不必检查 Spring 是否会关闭语句和连接,否则您需要自己处理。

关于spring - JDBCTemplate setQueryTimeout 特定于每个查询,即查询级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20889062/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com