這是Java的問題,而不是JDBC驅動程序。在某些情況下,套接字連接調用會忽略超時參數,並可能需要幾分鐘才能返回。當防火牆阻止端口時,這發生在我們身上。它發生在所有的TCP連接(HTTP,RMI)上。
我找到的唯一解決辦法就是打開在不同的線程這樣的連接,
private static final ExecutorService THREADPOOL
= Executors.newCachedThreadPool();
private static <T> T call(Callable<T> c, long timeout, TimeUnit timeUnit)
throws InterruptedException, ExecutionException, TimeoutException
{
FutureTask<T> t = new FutureTask<T>(c);
THREADPOOL.execute(t);
return t.get(timeout, timeUnit);
}
try {
Data data = call(new Callable<Data>() {
public Data call() throws Exception
{
// Open connection, get data here
return data;
}, 2, TimeUnit.SECONDS);
} catch (TimeoutException e) {
System.err.println("Data call timed-out");
}
這是一個選項,我已經打了,但它實際上相當於同樣的事情,放棄線程,因爲使用Thread.stop ()不影響阻塞I/O操作。 – Dan 2009-11-10 16:01:54