设置JDBC连接的网络超时 [英] Setting Network Timeout for JDBC connection

查看:2128
本文介绍了设置JDBC连接的网络超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试设置网络超时我的Oracle数据库连接在Java。但是,我收到一个错误。下面是示例代码及其各自的异常。

  try {
conn = new Database(oracle)。 ();
conn.setNetworkTimeout(null,30000); //我没有Executor,所以字段设置为空
System.out.println(Switch.date()+ - >数据库连接已初始化);
}
catch(SQLException ex){
Logger.getLogger(Switch.class.getName())。log(Level.SEVERE,null,ex);
}

我得到的例外是:



线程main中的异常java.lang.AbstractMethodError:oracle.jdbc.driver.T4CConnection.setNetworkTimeout(Ljava / util / concurrent / Executor; I)V $ < init>(Switch.java:524)
at ke.co.smart.Switch.main(Switch.java:161)
Java结果:1



我相信这与抽象的方法有关(阅读AbstractMethodError)。可能会导致这个错误,因为我只实现了我认为已经在Java中定义的方法,因此,不拒绝编译。



NB :如果有抽象方法,Java不允许编译具体类。

解决方案

setNetworkTimeout()在JDBC 4.1中引入,在JDBC 4.0中不存在。



您将需要ojdbc7,因为JDBC 4.1仅在使用 setNetworkTimeout()方法。



底层的问题是,在接下来的规范中添加方法会导致这些接口的老实现出现错误。






显然,Oracle还有一个JDBC驱动程序属性,可以修改套接字超时。



您也可以尝试使用此Oracle JDBC属性以设置套接字超时(如果您使用精简驱动程序):

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


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.

解决方案

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

这篇关于设置JDBC连接的网络超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆