'Timing out DriverManager.getConnection()?

I'm working with Java and mysql for database and I ran into a weird problem: One of my clients have a very unstable connection and sometimes packet loss can be high. Ok that's not software's fault I know, but I went there to test and, when the program calls "DriverManager.getConnection()" and the network connection gets unstable, that line gets to lock the application (or the given thread) by several minutes. I have added some logics of course to use another datasource for caching data locally then saving to the network host when possible, but, I can't often let the program hang for longer than 10s (And this method doesn't seem to have any timeout specification). So, I came out with a workaround like this:

public class CFGBanco implements Serializable {
    public String driver = "com.mysql.jdbc.Driver";
    public String host;
    public String url = "";
    public String proto = "jdbc:mysql://";
    public String database;
    public String user;
    public String password;
}

private static java.sql.Connection Connect(HostConfig dataHost) throws java.sql.SQLException, ClassNotFoundException
    {
        dataHost.url = dataHost.proto+dataHost.host;
        if(dataHost.database != null && !dataHost.database.equals("")) dataHost.url += "/"+dataHost.database;
        java.lang.Class.forName(dataHost.driver);
            ArrayList<Object> lh = new ArrayList<>();
            lh.add(0, null);
            Thread ConThread = new Thread(()-> {
                try {
                    lh.add(0, java.sql.DriverManager.getConnection(
                        dataHost.url, dataHost.user, dataHost.password));
                } catch(Exception x ) {
                    System.out.println(x.getMessage());
                }
            }, "ConnThread-"+SessId);
            ConThread.start();
            Thread TimeoutThread = new Thread(() -> {
                int c = 0;
                int delay = 100;
                try {
                    try {
                        do {
                            try {
                                if(t.isAlive())
                                    Thread.sleep(delay);
                                else
                                    break;
                            } catch(Exception x) {}
                        } while((c+=delay) < 10000);
                    } catch(Exception x){}
                } finally { 
                    try {
                        t.stop();
                    } catch(Exception x){}
                }
            }, "ConTimeout-"+SessId);
            TimeoutThread.start();
            try {
                ConThread.join();
            } catch(Exception x) {}
            if(lh.get(0) == null)
                throw new SQLException();
        return (Connection) lh.get(0);
    }

I call getConnection from another thread, then make a secondary "timeout" thread to watch it and then Join the calling thread to the ConThread. I have been getting results close to expected, indeed, but it got me wondering: Is there a better way to do this? Does the creation of 2 threads eat up much on system resources, enough to make this approach unpractical?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source