Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force timeout for DriverManager.getConnection() method call?

I have an application which will establish DB connection with MySQL and execute queries. Sometimes the DriverManager.getConnection() method call takes 2 seconds and sometimes it takes 30 seconds. Is there any way to control this method to timeout after 2 seconds?

DriverManager.setLoginTimeout() doesn't seem to work.

Actually, am able to set timeout for statement.executeQuery() by sleeping the thread for my timeout value and closing the connection after wakeup. But its the connection establishment part where I couldn't really set the timeout.

Would appreciate any help.

like image 474
ihavprobs Avatar asked Jan 31 '11 12:01

ihavprobs


2 Answers

If there's no other options, you could always just execute the call in a separate thread, which you abort/ignore if it doesn't finish in 2 secs.

EDIT Here's an example of what I was thinking:

public class Dummy extends Thread {
private volatile Connection conn = null;
@Override
public void run() {
    try {
        this.conn = DriverManager.getConnection("foobar") ;
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
static public Connection getConnection() {
    Dummy d = new Dummy() ;
    d.start() ;
    try {
        Thread.sleep(2000) ;
    } catch (InterruptedException e) {}
    return d.conn ;
}
}

Then you can just call the static Dummy.getConnection() method other places in your code. One drawback is that this method will always take 2 secs, but changing it to return immediately when the thread is finished isn't too hard.

like image 106
Rune Aamodt Avatar answered Oct 01 '22 23:10

Rune Aamodt


Thank you to codebolt, I don't know if it's the best solution but this works for me. A 10 seconds time out.

public class Dummy extends Thread {
             private volatile java.sql.Connection conn = null;
             private boolean sleep = true;
            @Override
             public void run() {
                 try {

                     String driver = "net.sourceforge.jtds.jdbc.Driver";
                     Class.forName(driver).newInstance();                       
                     //timeout
                     DriverManager.setLoginTimeout(10);
                     this.conn = DriverManager.getConnection(url, user, pwd);
                     sleep = false;
                 } catch (Exception e) {}
             }
             public java.sql.Connection getConnection() {
                 Dummy d = new Dummy() ;
                 d.start() ;
                 try {
                     for(int i=1; i<=10; i++) {
                         //Wait 1 second
                         if (d.sleep){
                             Thread.sleep(1000);  
                         }
                     }  
                 } catch (InterruptedException e) {}
                 return d.conn ;
             }
             }

And the call:

Dummy a = new Dummy();
connection = a.getConnection();
if (connection != null) {....
like image 44
anpadia Avatar answered Oct 01 '22 23:10

anpadia