Sub topic 2
Service names, SIDs, and the listener
This chapter assumes you know that an Oracle connection needs a host, a port, a service name, and credentials. It also assumes you can run a simple query. It explains the service name and the listener, which are the parts that cause most connection failures.
The listener's job
The listener is a separate process, usually tnslsnr, that runs on the database server. It does not run SQL and it does not check passwords. It accepts incoming network connections on a port (1521 by default) and hands each one to the right database instance.
The listener only routes a connection if the name in the request matches a service it knows about. That name is the service name in your connect string. A wrong host or port means the client never reaches a listener. A wrong service name means the client reaches a listener that has no route for the request.
Service names
A service name is a logical name for a workload on a database. The client asks for a service, not for a particular machine or instance. One service can run on several instances (Oracle RAC), and one instance can offer several services. You can move a service between instances without changing client connect strings.
Each database has default services, and an administrator can add more:
- A non-container database has a default service named after
DB_UNIQUE_NAMEplusDB_DOMAIN, for exampleorcl.example.com. - In a multitenant database, the container database (CDB) has its own default service, such as
orcl.example.com. Each pluggable database (PDB) has a default service named after the PDB, such asorclpdb1.example.com. - Administrators can create more services for a PDB. For example, a reporting service can run on a standby while an OLTP service runs on the primary.
Connect application and developer tools to a PDB service. Connecting to the CDB service puts you in CDB$ROOT, which is rarely what you want for ordinary work.
Listing the services a database offers
From any session with access to the dynamic views, run:
select name, network_name, pdb
from v$services
order by name;NETWORK_NAME is the string that clients put in SERVICE_NAME. It is the value to copy into your connect string. To see the instance-level setting, use show parameter service_names in SQLcl.
SIDs
The SID (system identifier) names a database instance, which is the set of memory structures and background processes. Older Oracle versions and many older tutorials connected by SID. A SID identifies one instance only, so it cannot represent a service that spans instances or a PDB.
| Row | Service name | SID |
|---|---|---|
| Identifies | A service offered by the database | One instance |
| Works with PDBs | Yes, each PDB has its own service | No |
| Works with RAC failover | Yes | No, it ties you to one instance |
| Easy Connect form | `host:1521/service_name` | Not supported in the standard form |
| `tnsnames.ora` keyword | `SERVICE_NAME = ...` | `SID = ...` |
| Use today | Preferred in all new connections | Legacy only |
In a tnsnames.ora entry, the keyword inside CONNECT_DATA decides which one you use:
ORCL_LEGACY =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = dbhost.example.com)(PORT = 1521))
(CONNECT_DATA = (SID = orcl))
)SQL Developer's Basic connection tab offers both a Service name option and a SID option. Choose Service name unless you are working with an older setup that documents a SID. A SID connection fails against a PDB.
How services get on the listener
The listener learns about services in one of two ways.
Dynamic registration
This is the default. The database's listener registration process (LREG in current releases, PMON in older ones) tells the listener which services the instance offers. It does this at startup and then periodically. You do not edit listener.ora for this.
Two details matter in practice:
- The database registers with the listener named by the
LOCAL_LISTENERparameter. If that parameter is empty, it tries the default listener on port 1521 on the same host. If your listener runs on another port, setLOCAL_LISTENERor registration silently misses it. - Registration is not instant. For a short time after the database starts, the listener is up but has no services, and clients get ORA-12514. To force registration, run this as a privileged user:
alter system register;Static registration
An administrator can list a service by hand in the SID_LIST_LISTENER section of listener.ora. This is mainly needed when the database is down and you must still reach it, for example to start it remotely as SYSDBA. A statically registered service shows in the listener as status UNKNOWN, because the listener cannot confirm that the instance is up.
Asking the listener what it knows
On the database server, lsnrctl queries the listener directly:
lsnrctl status
lsnrctl serviceslsnrctl status shows the listener's address, its log location, and a summary of services. lsnrctl services adds the instance and handler detail. A trimmed example looks like this:
Service "orclpdb1.example.com" has 1 instance(s).
Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orcl.example.com" has 1 instance(s).
Instance "orcl", status READY, has 1 handler(s) for this service...Compare the quoted service name with the name in your connect string, character by character. Instance status READY means the instance accepts connections. BLOCKED means it is up but not accepting new connections. UNKNOWN means a static entry.
Matching the error to the layer
The service name and listener explain the most common early failures. Each error tells you which layer failed.
| Error | Meaning | First thing to check |
|---|---|---|
| ORA-12541: TNS:no listener | Nothing answered on that host and port | Host name, port, firewall, listener running (`lsnrctl status`) |
| ORA-12514: listener does not currently know of service requested | A listener answered, but the service name is not registered | Spelling and domain suffix of the service name, database open, `LOCAL_LISTENER` |
| ORA-12505: listener does not currently know of SID given in connect descriptor | The request used a SID the listener does not know | Switch to a service name |
| ORA-12154: could not resolve the connect identifier | The client could not translate the alias to an address | `tnsnames.ora` location, alias spelling, `TNS_ADMIN` |
ORA-12154 happens on the client before any network call. The other three come from the network path. ORA-12541 and ORA-12514 are the pair to learn to tell apart. With ORA-12541 you never reached a listener. With ORA-12514 you did reach one, but it has no entry for the service name you sent. A later chapter covers the full troubleshooting workflow.
Putting it together in a connection
The same service name appears in every connect format. Only the syntax around it changes:
Easy Connect: hr@//dbhost.example.com:1521/orclpdb1.example.com
JDBC thin: jdbc:oracle:thin:@//dbhost.example.com:1521/orclpdb1.example.com
tnsnames.ora: (CONNECT_DATA = (SERVICE_NAME = orclpdb1.example.com))After connecting, confirm that the service you asked for is the service you got:
select sys_context('USERENV', 'SERVICE_NAME') as service_name,
sys_context('USERENV', 'CON_NAME') as container,
sys_context('USERENV', 'INSTANCE_NAME') as instance
from dual;If CON_NAME shows CDB$ROOT when you expected a PDB, you connected to the CDB service. Fix the service name in your connect string.
Exercise
A client connects with hr@//dbhost:1521/ORCLPDB1 and gets ORA-12514. Running lsnrctl services on the server shows only orclpdb1.example.com and orcl.example.com. What is the likely cause, and what are two ways to fix it?
Show answer
The service name lacks the domain suffix. The listener has registered orclpdb1.example.com, so a request for ORCLPDB1 finds no match. The first fix is to change the connect string to hr@//dbhost:1521/orclpdb1.example.com. The second is to have the administrator add a service named orclpdb1 to the PDB and then run alter system register. The first fix is simpler and keeps the setup unchanged.
Exercise
You connected to a database and sys_context('USERENV','CON_NAME') returns CDB$ROOT. Name the likely cause and the fix.
Show answer
The connect string used the CDB's default service instead of a PDB service. Query v$services (or run lsnrctl services) to find the PDB's service name, then reconnect using that name.
Exercise
A database was restarted a minute ago. lsnrctl status shows the listener running, but clients get ORA-12514. Why, and what can you do?
Show answer
The instance has not yet registered its services with the listener, which happens at startup and periodically. Wait for registration to complete, or connect as a privileged user and run alter system register;. Then check lsnrctl services again.