Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Network Timeout for JDBC connection

I'm trying to set a network time-out my Oracle database Connection in Java. However, I'm getting an error. Below is sample code and it's respective exception.

try{
    conn = new Database("oracle").connect();
    conn.setNetworkTimeout(null, 30000); //I don't have an Executor, so the field is set to null
    System.out.println(Switch.date() + " -> Database Connection Initialized");
}
catch(SQLException ex){
    Logger.getLogger(Switch.class.getName()).log(Level.SEVERE, null, ex);
}

The Exception I'm getting is:

Exception in thread "main" java.lang.AbstractMethodError:oracle.jdbc.driver.T4CConnection.setNetworkTimeout(Ljava/util/concurrent/Executor;I)V
   at ke.co.smart.Switch.<init>(Switch.java:524)
   at ke.co.smart.Switch.main(Switch.java:161)
Java Result: 1

I believe it has to do with a method being abstract (read AbstractMethodError). What could probably cause this error as I have only implemented the method which I think is already defined within Java, and thus, does not refuse to compile.

N.B.: Java does not allow compilation of concrete classes if there are abstract methods.

like image 383
Patrick W Avatar asked Sep 16 '13 07:09

Patrick W


People also ask

What is JDBC connection timeout?

Connection timeout means response timeout of any JDBC call invoking data transmission over a connection socket. If the response message is not received within the time specified, an I/O exception is thrown. The JDBC standard (2.0/3.0) does not support setting of the connection timeout.

What is the default connection pool size in JDBC?

The connection pool configuration settings are: Initial and Minimum Pool Size: Minimum and initial number of connections maintained in the pool (default is 8) Maximum Pool Size: Maximum number of connections that can be created to satisfy client requests (default is 32)


2 Answers

setNetworkTimeout() was introduced in JDBC 4.1 and was not present in JDBC 4.0.

You will want ojdbc7 since JDBC 4.1 only came in with Java 7 if you want to use setNetworkTimeout() method.

The underlying issue is that adding methods to interfaces in later specifications can cause older implementations of those interfaces to break with errors. One of the new features of the upcoming Java 8, default methods, will hopefully make this slightly less of a problem.


Apparently there is also a JDBC driver property for Oracle that can modify socket timeouts.

You can also try using this Oracle JDBC property to set the socket timeout if you are using the thin driver:

Properties props = new Properties();
props.setProperty("user", "dbuser");
props.setProperty("password", "dbpassword");
props.setProperty(OracleConnection.CONNECTION_PROPERTY_THIN_NET_CONNECT_TIMEOUT, "2000");

Connection con = DriverManager.getConnection("<JDBC connection string>", props);
like image 56
prunge Avatar answered Sep 20 '22 14:09

prunge


This is a classic case of software evolution. The JDBC provider has not given the implementation of the method yet in the jar you are using. Looks like your JDBC library is quite old and you may try the latest one.

Download latest one from here: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html

Try this approach taken from here:

conn.setNetworkTimeout(Executors.newFixedThreadPool(numThreads), yourTimeout);
like image 24
Gyanendra Dwivedi Avatar answered Sep 19 '22 14:09

Gyanendra Dwivedi