您可以通過JdbcTemplate
執行的方法允許您傳入PreparedStatementCreator
。您總是可以使用它攔截調用(可能使用Proxy
),該調用導致cancel
在單獨的線程上發生,某些cond
變爲true
。
public Results respondToUseRequest(Request req) {
final AtomicBoolean cond = new AtomicBoolean(false);
requestRegister.put(req, cond);
return jdbcTemplate.query(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection conn) {
PreparedStatement stmt = conn.prepareStatement();
return proxyPreparedStatement(stmt, cond);
}
},
new ResultSetExtractor() { ... });
}
這個canceller
本身可以在成功完成時被取消;例如
private final static ScheduledExecutorService scheduler =
Executors.newSingleThreadedScheduledExecutor();
PreparedStatement proxyPreparedStatement(final PreparedStatement s, AtomicBoolean cond) {
//InvocationHandler delegates invocations to the underlying statement
//but intercepts a query
InvocationHandler h = new InvocationHandler() {
public Object invoke(Object proxy, Method m, Object[] args) {
if (m.getName().equals("executeQuery") {
Runnable cancel = new Runnable() {
public void run() {
try {
synchronized (cond) {
while (!cond.get()) cond.wait();
s.cancel();
}
} catch (InterruptedException e) { }
}
}
Future<?> f = scheduler.submit(cancel);
try {
return m.invoke(s, args);
} finally {
//cancel the canceller upon succesful completion
if (!f.isDone()) f.cancel(true); //will cause interrupt
}
}
else {
return m.invoke(s, args);
}
}
}
return (PreparedStatement) Proxy.newProxyInstance(
getClass().getClassLoader(),
new Class[]{PreparedStatement.class},
h);
所以,現在是響應用戶的取消會是什麼樣代碼:
cond.set(true);
synchronized (cond) { cond.notifyAll(); }
一個有趣而奇怪的例子,並不完全確定如何將其應用於手頭的問題。很明顯,10秒鐘的自動取消將需要用外部觸發的東西來取代。 – skaffman 2009-06-29 07:25:40
爲什麼它必須被外部觸發? OP沒有提及任何關於它的事情,比如用戶定義的 – 2009-06-29 07:57:20