来自Oracle数据库12.2中运行的Java存储过程的JDBC连接无法正常工作

问题描述

尝试从在Oracle数据库中运行的Java存储过程创建Oracle数据库连接时,我们收到一条错误消息。我们已经隔离了该连接,并且它可以与运行Java 1.7的Oracle Database 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");添加代码中,则我们的连接参数正确并且连接成功。但是,如上所述,这带来了不可接受的性能开销。

解决方法

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

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

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