Sub topic 1
What an Oracle connection is made of
A connection to Oracle Database is a chain of parts. Each part can fail on its own, and each failure produces a different error. This chapter names the parts and shows how they fit together, so the later chapters on SQL Developer, SQLcl, and troubleshooting have a shared vocabulary.
Prerequisites
This chapter assumes you have the following:
- Basic familiarity with relational databases and SQL.
- Comfort using a terminal or command prompt.
- A reachable Oracle Database (local, container, or cloud) and an account on it.
- A supported Java runtime, or a SQL Developer or SQLcl download that bundles one. You do not need Java for this chapter, but later chapters do.
You do not need to have installed any client tool yet. This chapter is about concepts, with a few short examples you can run later.
The four parts of a connection
Every Oracle connection needs four pieces of information:
| Part | What it answers | Example |
|---|---|---|
| Host | Which machine runs the listener? | `dbhost.example.com` |
| Port | Which TCP port does the listener watch? | `1521` |
| Service name | Which database service do you want? | `orclpdb1.example.com` |
| Credentials | Who are you? | user `HR`, plus a password |
The first three parts locate a database service on the network. The credentials authenticate you once you have reached it. A client tool such as SQL Developer or SQLcl collects all four, then asks the database to open a session.
Two of these parts are easy to confuse with something else.
- The host and port identify the listener, not the database. The listener is a separate process that accepts incoming connection requests.
- The service name identifies a named entry point into a database. It is not the database name, and it is not the instance name.
What happens when you connect
A connection happens in two phases. In the first phase, the client reaches the listener and asks for a service. In the second phase, the listener hands the request to a database server process, and the database checks your credentials.
This split explains why errors fall into two groups:
- Errors before the handoff are about location. The host is wrong, the port is closed, the listener is down, or the service is unknown.
- Errors after the handoff are about identity. The password is wrong, the account is locked, or the user lacks the
CREATE SESSIONprivilege.
Later chapters map specific ORA- codes onto these two groups. Knowing which phase failed is usually the fastest way to narrow down a problem.
Service names and the listener
The listener keeps a list of services that databases have registered with it. When your request names orclpdb1.example.com, the listener looks that name up. If it finds a match, it routes your request. If it does not, it refuses the request.
A single listener can serve many services. A multitenant container database, for example, usually exposes one service for the container root and one for each pluggable database (PDB). You connect to a PDB by naming its service, not by naming the container.
Older material often uses a SID (system identifier) instead of a service name. A SID identifies one instance. A service name is a logical label that can map to one or more instances, which matters for clusters. Prefer service names. Use a SID only when you must connect to a legacy setup that offers nothing else.
Identifying the database: three notations
The host, port, and service name can be written in several ways. All of them carry the same three facts. The notation you use depends on the tool and on how much configuration you want to keep.
Easy Connect
Easy Connect writes the three facts as one string, with no configuration file:
//dbhost.example.com:1521/orclpdb1.example.comThe pattern is //host:port/service_name. It is quick to type and needs no setup, so it suits one-off connections and scripts.
TNS alias
A TNS alias is a short name that maps to a full description stored in a file called tnsnames.ora:
ORCLPDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = dbhost.example.com)(PORT = 1521))
(CONNECT_DATA = (SERVICE_NAME = orclpdb1.example.com))
)Once this entry exists, tools can connect with just hr@ORCLPDB. The alias hides the host, port, and service name, so you can change them in one file when a database moves. The cost is that every machine needs a copy of the file, and the tool must be able to find it.
JDBC URL
Java-based tools sometimes ask for a JDBC URL. The thin driver form carries the same three facts:
jdbc:oracle:thin:@//dbhost.example.com:1521/orclpdb1.example.comThis is the Easy Connect string with a jdbc:oracle:thin:@ prefix. You will meet it when a tool asks for a custom JDBC URL.
The table below compares the three.
| Notation | Needs a config file | Best for |
|---|---|---|
| Easy Connect | No | Quick connections and scripts |
| TNS alias | Yes, `tnsnames.ora` | Shared, named databases that may move |
| JDBC URL | No | Java tools that ask for a URL |
Where the tools fit
SQL Developer and SQLcl both build the same connection from the same four parts. SQL Developer is a graphical application that stores each connection as a named entry and shows a form with fields for host, port, and service name. SQLcl is a command-line tool that takes the same facts as a connect string. Neither tool changes the model. They differ in how you enter the parts and how you reuse them.
Checking where you actually connected
A successful login does not prove you reached the database you meant. A stale alias or a wrong default can send you to a different service with the same user name. After you connect, ask 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 query returns the user, the service, and the server host of the current session. Compare them with what you intended. A later chapter covers this check in more detail.
Common misunderstandings
- Thinking the port identifies the database. Port 1521 is a convention for the listener. Many services can share one port.
- Confusing the service name with the SID. Using one where the other is expected gives a "listener does not know" error.
- Assuming a working password means a working location. Credentials are checked only after you reach the database. A bad host fails before the password is ever used.
- Putting the password in the connect string. It works, but it leaves the password in shell history and process lists. Later chapters show safer options.
Exercise
A user reports that a connection to //dbhost.example.com:1521/orclpdb1.example.com fails, and the error mentions the service. In which of the two connection phases did it fail, and which of the four parts is the likely cause?
Show answer
It failed in the first phase, when the listener tried to match the request to a registered service. The likely cause is the service name, for example a typo, a missing domain suffix, or a service that is not registered with that listener. The credentials are not involved yet, because they are checked only in the second phase.
Exercise
You have the Easy Connect string //dbhost.example.com:1521/orclpdb1.example.com. Write the matching JDBC thin URL, and name the part of tnsnames.ora that holds the service name.
Show answer
The JDBC URL is jdbc:oracle:thin:@//dbhost.example.com:1521/orclpdb1.example.com. The service name sits in the CONNECT_DATA section, in the SERVICE_NAME setting.