During a session with the database, the client uses Oracle Call Interface (OCI) to interact with the database server. OCI is a software component that provides an interface between the client application and the SQL language the database server understands.

To connect to a database, we use the a "Connection String", which consists of three parts (separated by a "/" and a "@"); username, password, and the "TNS Alias" (or "Host String"). This is a typical Connection String: scott/tiger@orcl.

The TNS Alias needs to be resolved through a number of mechanisms that may be deployed in your network.

The most common mechanism for resolving the TNS Alias is the tnsnames.ora file, which has caused an unknown number of wasted hours for an unknown number of consultants and customers, and thereby an unknown amount of money.

Configuration

On the client, two files (sqlnet.ora and tnsnames.ora) contain information about available connections. These two files can be found in the \network\admin subfolder of the Oracle home folder, e.g. C:\ora\mt\network\admin.

Typical sqlnet.ora:

# sqlnet.ora Network Configuration File: C:\ora\mt\network\admin\sqlnet.ora
# Generated by Oracle configuration tools.

SQLNET.AUTHENTICATION_SERVICES= (NTS)

NAMES.DIRECTORY_PATH= (TNSNAMES, LDAP, EZCONNECT, ONAMES, HOSTNAME)

Typical tnsnames.ora:

# tnsnames.ora Network Configuration File: C:\ora\mt\network\admin\tnsnames.ora
# Generated by Oracle configuration tools.

ORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = server.company.com)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl.company.com)
    )
  )

BPEL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = server.company.com)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = bpel.company.com)
    )
  )

EXTPROC_CONNECTION_DATA =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = HOSTNAME)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = PLSExtProc)
    )
  )

Sometimes, the sqlnet.ora contains a default domain, e.g.:

NAMES.DEFAULT_DOMAIN = world

The complete TNS alias name is assembled by appending the value of the "NAMES.DEFAULT_DOMAIN" parameter from the sqlnet.ora file to the TNS alias name in the tnsnames.ora file. So, if the "NAMES.DEFAULT_DOMAIN" parameter is set to "world", the complete TNS alias for the "orcl.company.com" database service shown in the tnsnames.ora file above would be "orcl.world". Trying to connect using "orcl" would not work.

Tips