tags:

views:

368

answers:

4

Sometimes when I call connect() on a third-party proprietary JDBC driver, it never returns and a stack trace shows that it is stuck waiting for a socket read (usually). Is there a generic way to forcibly cancel this operation from another thread? It's a blocking I/O call so Thread.interrupt() won't work, and I can't directly close the socket because I don't have access to it since it's created inside the proprietary code.

I'm looking for generic solutions because I have a heterogeneous DB environment (Oracle, MySQL, Sybase etc). But driver-specific suggestions are also welcome. Thanks,

+1  A: 

At least one JDBC driver (not one of those you listed, though) will cleanly close the connection if the thread this connection attempt is running on is interrupted. I don't know if this will work for all drivers though.

Carl Smotricz
+1  A: 

Ah ... the joys of using closed-source libraries ...

If interrupt() doesn't work, and you cannot set some kind of timeout, then I think there is no safe way to do it. Calling Thread.kill() might do the job, but the method is deprecated because it is horribly unsafe. And this is the kind of scenario where the unsafe-ness of Thread,kill() could come back and bite you.

I suggest that you simply code your application to abandon the stuck thread. Assuming that your application doesn't repeatedly try to connect to the DB, a stuck thread isn't a huge overhead.

Alternatively use a better JDBC driver. (And on your way out of the door, complain to the supplier about their driver being too inflexible. There is a slight chance that someone might listen to you ...)

Stephen C
That's one option I've played with but it actually amounts to the same thing as abandoning the thread because Thread.stop() doesn't affect blocking I/O operations.
Dan
+1  A: 

There is no standard JDBC interface to set connection or read timeouts, so you are bound to use proprietary extensions, if the JDBC driver supports timeouts at all. For the Oracle JDBC thin driver, you can e.g. set the system properties "oracle.net.CONNECT_TIMEOUT" and or "oracle.jdbc.ReadTimeout" or pass a Properties instance to DriverManager.getConnection with these properties set. Although not particulary well documented, the Oracle specific properties are listed in the API documentation.

For other JDBC drivers, the documentation should contain the relevant references.

jarnbjo
A: 

This is a problem with Java, not the JDBC driver. In certain circumstances, the socket connect call ignores the timeout parameters and can take minutes to return. This happens to us when firewall blocks the port. It happens to all TCP connections (HTTP, RMI).

The only solution I find is to open connection in a different thread like this,

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");
   }
ZZ Coder
I'll try that. I can't just abandon the main thread because it's in the managed pool of a servlet container, but this should work.
Dan