【问题标题】:JDBC connection from Java stored procedure running in Oracle Database 12.2 is not working来自在 Oracle Database 12.2 中运行的 Java 存储过程的 JDBC 连接不起作用
【发布时间】: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 - 根据documentationIf 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.
  • @KentAnderson - 您可能会发现 this pagethis pagethis page 也很有用。

标签: java oracle oracle12c java-stored-procedures


【解决方案1】:

您很可能使用了错误的 URL “jdbc:oracle:thin:@server:port/SID”

应为 jdbc:oracle:thin:@serverhostname.com:1521/DB

@serverhostname.com oracle 数据库的主机名 1521 是你的数据库的一个端口(我认为 1521 是默认的) SID - DB 应为您的数据库的 SID(请向您的数据库管理员询问正确的)

【讨论】:

  • 我们使用了正确的主机名、端口和数据库标识符。出于隐私原因,我在发布的代码中使用这些值作为占位符。
猜你喜欢
  • 2015-08-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-13
  • 2016-06-20
相关资源
最近更新 更多