The only general solution to this problem is to have a separate thread where you try to get the connection. You can set a timeout to complete the task of getting that connection. In java, FutureTask can be used to accomplish this:
Connection connection;
FutureTask task = null;
try {
task = new FutureTask(new Callable() {
@Override
public Connection call() throws Exception {
return DriverManager.getConnection(url, username, password);
}
});
new Thread(task).start();
connection = task.get(getConnectionTimeout(), TimeUnit.SECONDS);
} catch (Exception e) {
throw new ConnectionException("Could not establish connection.", e);
}
No comments:
Post a Comment