简体   繁体   English

设置JDBC连接的网络超时

[英]Setting Network Timeout for JDBC connection

I'm trying to set a network time-out my Oracle database Connection in Java. 我正在尝试使用Java设置网络超时我的Oracle数据库连接。 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). 我认为它与抽象方法有关(读取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. 什么可能导致这个错误,因为我只实现了我认为已经在Java中定义的方法,因此,不拒绝编译。

NB: Java does not allow compilation of concrete classes if there are abstract methods. 注意:如果有抽象方法,Java不允许编译具体类。

setNetworkTimeout() was introduced in JDBC 4.1 and was not present in JDBC 4.0. setNetworkTimeout()是在JDBC 4.1中引入的,并且在JDBC 4.0中不存在。

You will want ojdbc7 since JDBC 4.1 only came in with Java 7 if you want to use setNetworkTimeout() method. 您将需要ojdbc7,因为如果您想使用setNetworkTimeout()方法,JDBC 4.1仅随Java 7一起提供。

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. 即将推出的Java 8默认方法的新功能之一,有望使这个问题略微减少。


Apparently there is also a JDBC driver property for Oracle that can modify socket timeouts. 显然,Oracle还有一个JDBC驱动程序属性,可以修改套接字超时。

You can also try using this Oracle JDBC property to set the socket timeout if you are using the thin driver: 如果使用瘦驱动程序,还可以尝试使用此Oracle JDBC属性设置套接字超时:

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);

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". 从Oracle的文档:“setNetworkTimeout抛出一个SQLException,如果:发生数据库访问错误,在关闭的连接上调用此方法,执行程序为NULL”。 The latter seems your case. 后者似乎是你的情况。

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. JDBC提供程序尚未在您使用的jar中提供该方法的实现。 Looks like your JDBC library is quite old and you may try the latest one. 看起来您的JDBC库已经很老了,您可以尝试使用最新的库。

Download latest one from here: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html 从这里下载最新版本: http//www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html

Try this approach taken from here : 尝试从这里采取这种方法:

conn.setNetworkTimeout(Executors.newFixedThreadPool(numThreads), yourTimeout);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM