Java – How to connect to the Oracle database using JDBC thin driver with TNSNames Alias Syntax

javajdbc

I am trying to connect to Oracle (11.2.0.2.0) database using the Oracle JDBC thin driver(ojdbc6.jar for 11.2.0.2.0) and the following JDBC URL syntax:

jdbc:oracle:thin:@abcd

where 'abcd' is defined in my tnsnames.ora file as shown below:

    abcd, abcd.world, abcd.dk.xyz.com  =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = abcd.dk.xyz.com)(PORT = 1521))
    (CONNECT_DATA =
      (SERVICE_NAME = abcd)
    )
  )

I have provided the VM option -Doracle.net.tns_admin=/etc/tnsnames.ora as per Oracle® Database JDBC Developer's Guide and Reference.

I am getting the following error when I run the application:

    Listener refused the connection with the following error:
ORA-12504, TNS:listener was not given the SID in CONNECT_DATA

But when I remove the aliases abcd.world and abcd.dk.xyz.com from tnsnames.ora, my application is able to connect to the database.

Is there an issue with ojdbc driver when there are multiple aliases in tnsnames.ora file?

My JDK version is 1.6.0_31.

Thanks,
VJ

Best Answer

It's look like, the Oracle JDBC driver can not work with multiple service names. Use these entry

    abcd =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = abcd.dk.xyz.com)(PORT = 1521))
    (CONNECT_DATA =
      (SERVICE_NAME = abcd)
    )
  )
    abcd.world, abcd.dk.xyz.com  =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = abcd.dk.xyz.com)(PORT = 1521))
    (CONNECT_DATA =
      (SERVICE_NAME = abcd)
    )
  )
Related Topic