从Java连接到Oracle不可靠

时间:2013-07-05 01:02:15

标签: java oracle

我是Java新手并尝试从redhat Linux机器设置与Oracle数据库的连接。我有连接工作,可以执行一个简单的SQL脚本,但连接只能工作约。 1/3次。它给出的错误和代码如下。很可能这是我们网络的一个问题,但我想在提出支持案例之前有更多的知识。当“someserver”是连接中指定的内容时,是否有人知道为什么错误为ssdb0255n21-oravip说“名称或服务未知”?

Exception in thread "main" java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:673)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:715)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:385)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:30)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:564)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:187)
    at OracleTest.main(OracleTest.java:19)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:445)
    at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:464)
    at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:594)
    at oracle.net.ns.NSProtocolStream.negotiateConnection(NSProtocolStream.java:214)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:263)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1360)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:486)
    ... 7 more
Caused by: java.net.UnknownHostException: ssdb0255n21-oravip: Name or service not known
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:894)
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1286)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1239)
    at java.net.InetAddress.getAllByName(InetAddress.java:1155)
    at java.net.InetAddress.getAllByName(InetAddress.java:1091)
    at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:117)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:133)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:411)
    ... 13 more

我正在使用的代码就是这个

import java.sql.*;
import java.util.Properties;

public class OracleTest
{
  public static void main(String[] args) throws SQLException
  {
    String url = "jdbc:oracle:thin:@ldaps://someserver:3131/ODBF2_BLOOD_P_S_B_001,cn=OracleContext,dc=in,dc=companyname,dc=com,dc=au";

    //properties for creating connection to Oracle database
    Properties props = new Properties();
    props.setProperty("user", "xxx");
    props.setProperty("password", "xxxx");

    //creating connection to Oracle database using JDBC
    System.out.println(DriverManager.getLoginTimeout());
    Connection conn = DriverManager.getConnection(url,props);

    String sql ="select sysdate as current_day from dual";

    //creating PreparedStatement object to execute query
    PreparedStatement preStatement = conn.prepareStatement(sql);

    ResultSet result = preStatement.executeQuery();

    while(result.next())
    {
      System.out.println("Current Date from Oracle : " +         result.getString("current_day"));
    }
    System.out.println("done");

  }
}

我将ORACLE_HOME设置为/usr/lib/oracle/10.2.0.5/client64/,并在/usr/lib/oracle/10.2.0.5/client64/network/admin中创建了ldap.ora和sqlnet.ora文件。这些内容包括:

SQLNET.ORA

NAMES.DIRECTORY_PATH = (LDAP,TNSNAMES)

ldap.ora

DIRECTORY_SERVERS = (OID.GLB.IN.companyname.COM.AU::3131)
DEFAULT_ADMIN_CONTEXT = "dc=in,dc=companyname,dc=com,dc=au"
DIRECTORY_SERVER_TYPE = OID

它似乎创建了tnsnames.ora本身

ORA11 =
 (DESCRIPTION =
   (ADDRESS_LIST =
     (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
   )
 (CONNECT_DATA =
   (SERVICE_NAME = ORA11)
 )
)

1 个答案:

答案 0 :(得分:0)

我讨厌回答我自己的问题,但事实证明这不是Oracle的问题。这是DNS解决失败的问题。我将ssdb0255n21-oravip主机放入/ etc / hosts文件中,问题立即消失。我怀疑某处的负载均衡器将50%的DNS查找指向返回错误的DNS服务器。我们这里有一个非常复杂的网络: - )