一.问题:有时我们执行的DB查询(例如一些查询的存储过程)会时间太长,当我们的代码逻辑上不想等待这么长时间时,怎么删除呢?
二.思考:首先想到使用"select connectionId()",然后通过"kill query id"语句来删除掉对应的DB进程.后来发现JdbcTemplate里面CallableStatement的cancel()可以关掉存储过程的调用.那么我们可以结合Timer来计时,当超过N分钟后,就执行cancel()即可.
三.代码:
1.定义一个task:(可以配合connectionId来做追踪,虽然逻辑上没有使用上)
public class CustomTimeTask extends TimerTask {
private static final Logger logger = LoggerFactory.getLogger(CustomTimeTask.class);
CallableStatement cs ;
int id;
public CustomTimeTask(CallableStatement cs, int id) {
super();
this.cs = cs;
this.id = id;
}
@Override
public void run() {
try {
logger.info("[Timeout]TimeTask try to cancel the long time procedure query. Connection ID is [{}]", id);
cs.cancel();
} catch (Exception e) {
logger.error("[Timeout]TimeTask fail to cancel the long time procedure query. Please check the if the q