Sub topic 3
Ways to identify a database: Easy Connect, TNS alias, JDBC URL
This chapter assumes you know the parts of an Oracle connection (host, port, service name, credentials) and the role of the listener from the earlier chapters. It covers the three notations you will meet most often when a tool asks "which database?": Easy Connect, a TNS alias, and a JDBC URL. All three carry the same information and differ only in where it is written down.
One destination, three notations
A client needs three facts to reach a database: the host, the port, and the service name. A connect identifier is the short piece of text that supplies them. The listener never sees the notation. The client expands it into a full connect descriptor and sends that to the listener.
| Notation | Where the details live | Typical user |
|---|---|---|
| Easy Connect | In the string you type | SQLcl, scripts, quick tests |
| TNS alias | In a `tnsnames.ora` file, looked up by name | SQL Developer, OCI-based tools, shared team setups |
| JDBC URL | In the URL a Java program or tool is given | Applications, tools that ask for a custom JDBC URL |
Easy Connect
Easy Connect needs no configuration file. You write the destination directly:
host[:port][/service_name]The port defaults to 1521 when you leave it out. A leading // is optional in SQLcl and SQL*Plus, but this tutorial keeps it because other tools require it. Starting SQLcl with an Easy Connect string looks like this:
sql hr@//dbhost.example.com:1521/orclpdb1.example.comSQLcl prompts for the password. Typing the password on the command line (hr/secret@...) puts it in your shell history and the process list, so avoid that form.
Use the fully qualified service name that the listener reports. A pluggable database usually registers a name such as orclpdb1.example.com, and a short form may not match. If you are unsure, ask the DBA or run lsnrctl services on the server.
Easy Connect Plus
Oracle Database 19c clients extend Easy Connect with extra syntax. This is called Easy Connect Plus. It adds a protocol prefix and name-value parameters:
sql hr@tcps://dbhost.example.com:1522/orclpdb1.example.com?wallet_location=/opt/walletThe tcps prefix selects TLS-encrypted transport. Parameters after ? replace settings that would otherwise live in tnsnames.ora or sqlnet.ora. The client library does the parsing, so the form works only if the client is 19c or later. Older clients reject it, often with ORA-12154.
Limits of Easy Connect
- Quote or escape the string if it contains special characters such as
?or&, because your shell interprets them. - It cannot express complex settings such as multiple addresses with failover unless you use Easy Connect Plus parameters.
- Every user must type or paste the full string, so mistakes spread through copies.
TNS alias and tnsnames.ora
A TNS alias (also called a net service name) is a short name that maps to a full connect descriptor. The mapping lives in a text file called tnsnames.ora:
ORCLPDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = dbhost.example.com)(PORT = 1521))
(CONNECT_DATA = (SERVICE_NAME = orclpdb1.example.com))
)With this entry, hr@ORCLPDB is enough. If the host moves, you edit one file and no connection string changes. That central control is the main reason teams use aliases.
Where the client finds tnsnames.ora
The client searches for tnsnames.ora in this order:
- The directory in the
TNS_ADMINenvironment variable, if it is set. network/adminunder the Oracle home or client install, if one exists.
SQLcl and SQL Developer run on Java and often have no Oracle home, so TNS_ADMIN is the dependable choice. Set it before you start the tool:
export TNS_ADMIN=/home/alice/network_admin
sql hr@ORCLPDBOn Windows, use set TNS_ADMIN=C:\oracle\network\admin in a command prompt, or set a user environment variable. In SQL Developer you can instead set the directory under Tools > Preferences > Database > Advanced > Tnsnames Directory.
Name resolution order
The NAMES.DIRECTORY_PATH parameter in sqlnet.ora controls which methods the client tries and in what order. A typical value is (TNSNAMES, EZCONNECT). If you list only TNSNAMES, Easy Connect strings stop working, and the failing attempt is reported as ORA-12154.
NAMES.DIRECTORY_PATH = (TNSNAMES, EZCONNECT)The client must also read the file without a syntax error. A missing parenthesis in one entry can make that entry, or all entries after it, unresolvable. Compare the parentheses first when an alias that looks correct fails.
Alias names and domains
If sqlnet.ora sets NAMES.DEFAULT_DOMAIN, the client appends that domain to the alias you type. Typing ORCLPDB then searches for ORCLPDB.example.com. This surprises people when the file defines the short name only. Either define both forms or remove the default domain.
JDBC URL
JDBC is the Java database API, and the Oracle thin driver is a pure-Java implementation that needs no Oracle client install. SQL Developer and SQLcl both use it. A JDBC URL for the same destination looks like this:
jdbc:oracle:thin:@//dbhost.example.com:1521/orclpdb1.example.comThe parts are the protocol (jdbc:oracle:thin), an @, and then an Easy Connect style //host:port/service. Tools such as SQL Developer ask for this form only when you pick a custom JDBC connection type. Applications put it in configuration files or connection pool settings.
The SID form and why to avoid it
An older URL form uses a colon instead of a slash and names a SID (system identifier), the name of one database instance:
jdbc:oracle:thin:@dbhost.example.com:1521:ORCLThe difference between //host:port/name and host:port:name is one character, and the two mean different things. The slash form takes a service name. The colon form takes a SID. A pluggable database has a service name but no SID, so the colon form cannot reach it. Prefer the slash form unless the DBA tells you a SID is the only option.
Using an alias in a JDBC URL
The thin driver can resolve a TNS alias too. You must tell it where tnsnames.ora is, either with a URL parameter or a Java property:
jdbc:oracle:thin:@ORCLPDB?TNS_ADMIN=/home/alice/network_adminWithout one of these, the driver does not search an Oracle home and reports that it cannot resolve the name. The URL parameter form requires a recent driver (18.3 or later). On older drivers, set -Doracle.net.tns_admin=/home/alice/network_admin on the Java command line.
Choosing a notation
- Use Easy Connect for a one-off test, a script, or a container on your laptop. It has no files to maintain.
- Use a TNS alias when many people or tools connect to the same database. One file holds the details, and tools that read
tnsnames.orashare them. - Use a JDBC URL when a tool or program asks for one. Copy host, port, and service name from a connection that already works so you do not retype them.
Confirm the notation reached the right place
A connect identifier that parses correctly can still point at the wrong database. After connecting, check the session:
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;The service name returned should match the one you wrote in the identifier. If it differs, the listener routed you to another service, which can happen with a service alias or a load-balanced name.
Common errors by notation
| Error | Likely cause with this notation |
|---|---|
| ORA-12154 | Alias not found. Wrong `TNS_ADMIN`, typo in the alias, a syntax error in `tnsnames.ora`, or `EZCONNECT` missing from `NAMES.DIRECTORY_PATH` |
| ORA-12514 | The listener does not know the service. Check spelling and whether the service name is fully qualified |
| ORA-12541 | Nothing is listening at that host and port. Check the host, port, and whether the listener is running |
Easy Connect and JDBC strings never trigger a file lookup, so ORA-12154 from them points to a malformed string or an old client. The full troubleshooting steps come in a later chapter.
Exercise
A teammate's SQLcl session runs sql hr@ORCLPDB and fails with ORA-12154, but sql hr@//dbhost.example.com:1521/orclpdb1.example.com works from the same machine. Name the most likely cause and one way to fix it without changing the alias.
Show answer
The client cannot find or read the tnsnames.ora that defines ORCLPDB, because Easy Connect needs no file and works. The usual cause is that TNS_ADMIN is unset or points to the wrong directory. Set TNS_ADMIN to the directory that contains tnsnames.ora and start SQLcl again. A syntax error in the file or a default domain in sqlnet.ora that changes the alias name are other possible causes.
Exercise
Write the JDBC URL for host db1.example.com, port 1521, and service name salespdb.example.com. Why would jdbc:oracle:thin:@db1.example.com:1521:salespdb fail against a pluggable database?
Show answer
jdbc:oracle:thin:@//db1.example.com:1521/salespdb.example.com. The colon form names a SID, and a pluggable database has a service name but no SID, so the listener cannot match it.