Setting Network Timeout for JDBC connection

前端 未结 3 514
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 10:47

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.

<         


        
相关标签:
3条回答
  • 2020-11-28 11:10

    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);
    
    0 讨论(0)
  • 2020-11-28 11:22

    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);
    
    0 讨论(0)
  • 2020-11-28 11:34

    From the Oracle's documentation: "setNetworkTimeout throws an SQLException if: a database access error occurs, this method is called on a closed connection, the executor is NULL". The latter seems your case.

    0 讨论(0)
提交回复
热议问题