The T-Files


Wed, 31 Oct 2007

tnsnames.ora Rant

Even Oracle supporters have to concede that it is just horribly hard to use, especially in its command line interfaces. I am not complaining (at least not this time) about the fact that you basically need to be a DBA in order to get anything done. It is a defensible position to require trained professionals to install such a complex system. I am talking about things like sensible default values, helpful diagnostic output, auto-completion, command history, adherence to generally acknowledged conventions and such.

I spent an hour today supporting one of our developers in fixing his Oracle connection problems, which turned out to be something extremely silly. Without the help of Google this could have potentially taken much longer.

There are a number of ways to connect to an Oracle DB using Oracle's own client software, the most popular one involves a text file tnsnames.ora that defines where the databases are.

MY_DATABASE =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = out.there)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = XE)
    )
  )

Even though the settings looked okay, we could not connect to the database.

$ sqlplus me@MY_DATABASE
ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified

This message seemed to indicate that tnsnames.ora was somehow broken, or not being read. But on the other hand, Oracle's network ping tool (which reads the same file) had no problem.

$ tnsping MY_DATABASE

Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = out.there)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE)))
OK (0 msec)

So maybe the file was okay? It was not, of course. But only because Oracle turned out to be extremely picky. tnsnames.ora had been copied from a Windows machine, and the Linux Oracle client did not like the Windows line breaks. Even if we accept that such behaviour is acceptable, having some of the tools handle the situation differently than others is very confusing.