Sub topic 6
Using a TNS alias and tnsnames.ora in SQL Developer
This chapter assumes you can already create a Basic connection in SQL Developer and that you know what a host, port, and service name are. It covers the TNS connection type: how a short alias becomes a network address, where SQL Developer looks for the file that defines it, and how to fix the errors you are most likely to see.
What a TNS alias does
A TNS alias is a short name, such as ORCLPDB, that stands for a full connect descriptor. A connect descriptor holds the host, port, and service name. The aliases are defined in a text file named tnsnames.ora.
A Basic connection stores host, port, and service name inside the SQL Developer connection itself. A TNS connection stores only the alias. The address details live in one shared file. If the database moves to a new host, you edit one file instead of every saved connection.
Here is an entry that maps the alias ORCLPDB to a host, port, and service name:
ORCLPDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = dbhost.example.com)(PORT = 1521))
(CONNECT_DATA = (SERVICE_NAME = orclpdb1.example.com))
)With this entry in place, hr@ORCLPDB identifies the same database as hr@//dbhost.example.com:1521/orclpdb1.example.com.
Where tnsnames.ora lives
The file is not part of SQL Developer. It belongs to Oracle Net configuration. Common locations are:
$ORACLE_HOME/network/admin/tnsnames.oraon a database server or full client.- A directory of your choice that the
TNS_ADMINenvironment variable points to. - A directory you set inside SQL Developer (see below).
SQL Developer connects with the JDBC thin driver, which is Java code that does not need an Oracle client installation. The thin driver still reads tnsnames.ora itself, so you only need to tell SQL Developer which directory holds it.
Setting the TNS directory in SQL Developer
- Open Tools > Preferences.
- Expand Database and select Advanced.
- In Tnsnames Directory, enter or browse to the directory that contains
tnsnames.ora. Enter the directory, not the file. - Click OK.
If this field is empty, SQL Developer falls back to the TNS_ADMIN environment variable and then to locations under ORACLE_HOME. An explicit setting removes guesswork, so prefer it when several Oracle homes exist on one machine.
You can also pass the directory as a Java option. Add this line to sqldeveloper.conf, adjusting the path:
AddVMOption -Doracle.net.tns_admin=/home/you/oracle/network/adminThis is useful on a shared workstation, because it ties the setting to one SQL Developer installation instead of to your user environment.
Creating a TNS connection
- In the Connections panel, click the green plus icon (New Connection).
- Enter a Name. This is only a label inside SQL Developer. It does not need to match the alias.
- Enter the Username and Password.
- Set Connection Type to TNS.
- Open the Network Alias drop-down and select
ORCLPDB. The list is filled from thetnsnames.orafile SQL Developer found. - Click Test. The status area at the bottom left of the dialog should say
Status: Success. - Click Save, then Connect.
If the drop-down is empty, SQL Developer did not find a usable file. Check the Tnsnames Directory preference first.
You can also type an alias into the Network Alias field instead of picking it from the list. This helps when the alias lives in a file you have not configured yet, but the test will then fail until the directory is right.
Verifying the session
A successful connection does not prove it went where you intended, especially if several aliases or files exist. Run this query in a worksheet:
select sys_context('USERENV', 'SESSION_USER') as session_user,
sys_context('USERENV', 'SERVICE_NAME') as service_name,
sys_context('USERENV', 'SERVER_HOST') as server_host
from dual;Compare service_name with the SERVICE_NAME in your descriptor. For a pluggable database the value should match the PDB service, such as orclpdb1.example.com, not the root container.
More than one address: load balancing and failover
A descriptor can list several addresses. Use an ADDRESS_LIST for this:
ORCLHA =
(DESCRIPTION =
(ADDRESS_LIST =
(LOAD_BALANCE = off)
(FAILOVER = on)
(ADDRESS = (PROTOCOL = TCP)(HOST = dbhost1.example.com)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = dbhost2.example.com)(PORT = 1521))
)
(CONNECT_DATA = (SERVICE_NAME = orclpdb1.example.com))
)With FAILOVER = on and LOAD_BALANCE = off, the driver tries the first address and moves to the next only if the first cannot be reached. This failover applies only to the moment of connecting. An open session is not moved to the other host. You cannot express this with a Basic connection, which is a common reason to use TNS.
Edge cases and gotchas
- Syntax is strict about parentheses. A missing closing parenthesis can make one alias, or every alias below it, disappear from the drop-down. Count parentheses when an alias you just added does not show up.
- Duplicate aliases. If the same alias appears twice, the first match wins. A stale entry higher in the file can send you to an old host.
- Aliases are case-insensitive.
orclpdbandORCLPDBmatch. - Domain suffixes. If
sqlnet.orasetsNAMES.DEFAULT_DOMAIN, Oracle Net appends that domain to an alias that has no dot. An alias then may need to be defined asORCLPDB.example.com. A file where aliases have one form and the client expects another produces ORA-12154. - Name resolution order.
NAMES.DIRECTORY_PATHinsqlnet.oracontrols which methods are tried (for exampleTNSNAMESandEZCONNECT). If it lists onlyLDAP, your file is never read. - Included files. An
IFILE=/path/to/other.oraline intnsnames.orapulls in more aliases. If the included file is unreadable, those aliases are missing without a clear message. - Edits and the open dialog. SQL Developer reads the file when it needs the alias list. After editing the file, close and reopen the New Connection dialog. If the new alias still does not appear, restart SQL Developer.
- Several files. A copy of
tnsnames.orain your home directory is not read unless the Tnsnames Directory orTNS_ADMINpoints to it. Editing the wrong copy is the most common cause of "I added it but nothing changed".
Troubleshooting TNS connections
The error code tells you which stage failed. Work from the alias outward.
| Error | Stage that failed | What to check |
|---|---|---|
| ORA-12154: TNS:could not resolve the connect identifier specified | Alias lookup | Tnsnames Directory preference, `TNS_ADMIN`, spelling of the alias, parentheses, `NAMES.DEFAULT_DOMAIN` |
| ORA-12541: TNS:no listener | Network contact | Host and port in the descriptor, listener running, firewall |
| ORA-12514: TNS:listener does not currently know of service requested | Listener to service | `SERVICE_NAME` spelling, domain part, PDB open, service registered with the listener |
| ORA-01017: invalid username/password; logon denied | Authentication | Username, password, case-sensitive password, locked or expired account |
The sequence matters. ORA-12154 means SQL Developer never reached the network. ORA-12541 means it reached the host but nothing listened on that port. ORA-12514 means a listener answered but did not offer your service. ORA-01017 means you reached the database and the credentials were refused, so the TNS file is fine.
Exercise
You add a new alias SALESPDB to the tnsnames.ora in your home directory, but the Network Alias drop-down in SQL Developer does not list it, and typing it gives ORA-12154. Name two things to check, and say which error you would expect if the file were found but the listener had no such service.
Show answer
First, check that the Tnsnames Directory preference (Tools > Preferences > Database > Advanced) or the TNSADMIN variable points to the directory holding the edited file, and that you edited the copy SQL Developer reads. Second, check the syntax of the new entry, since an unbalanced parenthesis can hide it, and check for NAMES.DEFAULTDOMAIN in sqlnet.ora. If the file were found and the listener were reached but did not know the service, you would get ORA-12514 instead of ORA-12154.
Exercise
A colleague says a TNS connection and a Basic connection are different ways to reach different kinds of databases. Is that correct? Give one practical reason to choose TNS.
Show answer
No. Both reach the same database through the same host, port, and service name. They differ only in where those values are stored: inside the SQL Developer connection (Basic) or in tnsnames.ora (TNS). TNS is useful when the address changes often and should be edited in one place, or when the descriptor needs features Basic cannot express, such as several addresses with failover.