【发布时间】:2020-12-07 20:24:54
【问题描述】:
我们在尝试从 Oracle 数据库中运行的 Java 存储过程创建 Oracle 数据库连接时收到错误消息。我们已经隔离了连接,它正在运行 Java 1.7 的 Oracle 数据库 12.1。自从升级到 Oracle Database 12.2 和 Java 1.8 后,我们收到以下错误消息。
ORA-29532: Java call terminated by uncaught Java exception:
java.lang.RuntimeException: IO Error: The Network Adapter could not establish the connection
Attempting to connect with: jdbc:oracle:thin@########
An error occurred in ###: java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
这是我们用来测试连接的代码
import java.sql.Connection;
import java.sql.SQLException;
import oracle.jdbc.pool.OracleDataSource;
public class RemoteDBTest {
public static String remoteConnection()
throws SQLException
{
StringBuffer sb = new StringBuffer();
String userId = "xxxx";
String password = "xxxxx";
String url = "jdbc:oracle:thin:@server:port/SID"; // Destination is a remote database. Actual host, port, and service have been replaced in this example.
Connection conn = null;
OracleDataSource ods = new OracleDataSource();
ods.setUser(userId);
ods.setPassword(password);
ods.setURL(url);
System.out.println(url);
System.out.println(System.currentTimeMillis());
try {
conn = ods.getConnection();
} catch (Exception e){
System.out.println(System.currentTimeMillis());
throw e;
}
System.out.println(System.currentTimeMillis());
sb.append("Auto commit = " + conn.getAutoCommit());
conn.close();
return sb.toString();
}
public static void main(String[] args) {
try{
System.out.println(remoteConnection());
} catch (SQLException e){
e.printStackTrace();
}
}
}
此时,Oracle 支持的唯一有效解决方法是将属性 java.net.preferIPv4Stack 设置为 true,但是,当通过代码设置时,它会将连接的执行时间增加到 9 秒。我们尝试通过命令行和 _JAVA_OPTIONS 环境变量来设置这个属性,但是,它似乎不会影响运行存储过程的 JVM。
任何想法或建议将不胜感激。
主机操作系统:Windows Server 2016
Oracle Database 12c 企业版 12.2.0.1.0 - 64 位生产
注意:这不是 IO Error: The Network Adapter could not establish the connection 的副本。如果我们在代码中添加System.setProperty("java.net.preferIPv4Stack" , "true");,我们的连接参数是正确的并且连接成功。但是,如上所述,这会带来不可接受的性能开销。
【问题讨论】:
-
@Abra 是的,它是一个java存储过程。不是pl/sql。
-
@Abra 你去吧
-
@Abra - 问题是
We are getting an error message when attempting to create an Oracle db connection from Java code...。 -
@KentAnderson - 根据documentation:
If IPv6 is available on the operating system, the underlying native socket will be an IPv6 socket. This allows Java applications to connect to, and accept connections from, both IPv4 and IPv6 hosts. If an application has a preference to only use IPv4 sockets, then this property can be set to true. The implication is that the application will not be able to communicate with IPv6 hosts.
标签: java oracle oracle12c java-stored-procedures