Java 为 JDBC 连接设置网络超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18822552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting Network Timeout for JDBC connection
提问by Patrick W
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.
我正在尝试在 Java 中设置我的 Oracle 数据库连接的网络超时。但是,我收到一个错误。下面是示例代码,它是各自的例外。
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.
我相信这与抽象的方法有关(阅读 AbstractMethodError)。什么可能导致此错误,因为我只实现了我认为已经在 Java 中定义的方法,因此不会拒绝编译。
N.B.: Java does not allow compilation of concrete classes if there are abstract methods.
注意:如果有抽象方法,Java 不允许编译具体类。
采纳答案by prunge
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.
如果您想使用setNetworkTimeout()
方法,您将需要 ojdbc7,因为 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 propertyto 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);
回答by Gyanendra Dwivedi
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.
这是软件进化的一个经典案例。JDBC 提供程序尚未在您使用的 jar 中提供该方法的实现。看起来您的 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);
回答by Gabe
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.
来自 Oracle 的文档:“setNetworkTimeout 在以下情况下抛出 SQLException:发生数据库访问错误,在关闭的连接上调用此方法,执行程序为 NULL”。后者似乎是你的情况。