这是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