Setting Network Timeout for JDBC Connection

I am trying to set the network timeout of my Oracle Connection database in Java. However, I am getting an error. The following is sample code and the corresponding 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 that I get 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 this is because the method is abstract (read AbstractMethodError). Which may cause this error, since I only implemented a method that I think is already defined in Java, and therefore does not refuse to compile.

NB: Java does not allow compilation of specific classes if abstract methods exist.

+8
source share
3 answers

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

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

The main problem is that adding methods to interfaces in later specifications can break older versions of these interfaces with errors. One of the new features of upcoming Java 8, the default methods, hopefully will make this a little less problem.


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

You can also try using this Oracle JDBC property to set the socket timeout if you are using a 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); 
+11
source

From the Oracle documentation: "setNetworkTimeout throws a SQLException if: a database access error occurs, this method is called when the connection is closed, the executor is NULL." The latter seems to be your business.

+1
source

This is a classic case of software evolution. The JDBC provider has not provided an implementation of the method yet in the bank used. It looks like your JDBC library is pretty old and you can try the latest version.

Download the latest version 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); 
0
source

Source: https://habr.com/ru/post/917620/


All Articles