2

I tried to set a connection timeout with the following code:

public class ConnectionTimeout {  
  public static void main(String[] args) throws Exception {

    String entry = "jdbc:oracle:thin:@xxx:1521:xxx";
    Connection con=null;

    Class.forName("oracle.jdbc.driver.OracleDriver");

    DriverManager.setLoginTimeout(1);        
    con=DriverManager.getConnection(entry,"username","password");    

    Statement s=con.createStatement();    
    s.execute("select 1 from dual");    
    s.close();

    con.close();
  }
}

The instance xxx is not existing. But I get the following exception:

Exception in thread "main" java.sql.SQLException: E/A-Exception: Socket is not connected
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:439)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at my.package.connection.timeout.ConnectionTimeout.main(ConnectionTimeout.java:22)

How I can implement a timeout to an not existing or available Oracle database instance?

Edit: If I set the DriverManager.setLoginTimeout(30); to 30 second, the exception happens so fast as before!

2 Answers 2

4

Your DriverManager.setLoginTimeout(1); sets the maximum time in seconds for the driver to wait while connecting to the database. In your case, it's set to 1.

To have no limit, set setLoginTimeout(0) where 0 means no limit.

I hope this helps.


Update if your instance xxx doesn't exists, how would you expect your Oracle Driver to connect to the database? It won't make any difference how long you set your loginTimeout there's not "host" to connect to.

Sign up to request clarification or add additional context in comments.

2 Comments

That is not was I mean. I want a small timeout for trying to etablish a connection. I'm confusing about when I set the timeout to 30 seconds! With 1 and 30 secondes, the exception comes immediately. Or is that OK in my test code?
@Tim Krueger, see updated post. That's why the response is immediate, there's no host.
0

Because, in Java doc, it shows: timeout in seconds, but in the implementation of JDBC Oracle, it is milliseconds.

You can try using a measure of milliseconds.

1 Comment

Is this correct? I tried connecting with the timeout set to 1 in a loop, and never got a timeout. If it would be 1 millisecond, that would surprise me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.