Sub topic 9
Verifying Who and Where You Are Connected
This chapter assumes that you know basic SQL, can run a query in SQL Developer or SQLcl, and have access to an Oracle Database account. The goal is to confirm the identity and location of your current session before you run other commands.
A successful login does not always mean that you reached the database you intended. Development, test, and production systems can use similar usernames and service names. Verify the session explicitly.
Inspecting the Current Session
Oracle provides session information through SYS_CONTEXT. This function returns a value from the current session environment. The USERENV namespace contains connection details such as the logged-in user, service, and server host.
Run this query in SQL Developer or SQLcl:
select sys_context('USERENV', 'SESSION_USER') as session_user,
sys_context('USERENV', 'CURRENT_USER') as current_user,
sys_context('USERENV', 'SERVICE_NAME') as service_name,
sys_context('USERENV', 'SERVER_HOST') as server_host
from dual;DUAL is a special one-row table supplied by Oracle. It lets you select expressions when you do not need to read application data.
The result might look like this:
| SESSION_USER | CURRENT_USER | SERVICE_NAME | SERVER_HOST |
|---|---|---|---|
| HR | HR | ORCLPDB1.EXAMPLE.COM | DBHOST |
SESSION_USER is the user who authenticated when the session was created. CURRENT_USER is the user Oracle currently uses for name resolution and authorization. They are normally the same for a simple connection, but they can differ while executing stored code or after changing execution context.
SERVICE_NAME identifies the database service to which the client connected. A service is a named endpoint that a listener offers to clients. SERVER_HOST identifies the database server handling the session.
You can request more details when needed:
select sys_context('USERENV', 'INSTANCE_NAME') as instance_name,
sys_context('USERENV', 'INSTANCE') as instance_number,
sys_context('USERENV', 'DB_UNIQUE_NAME') as db_unique_name,
sys_context('USERENV', 'SID') as session_id
from dual;INSTANCE_NAME identifies the Oracle instance. DB_UNIQUE_NAME identifies the database in the database configuration. In a clustered environment, the instance and server can vary between sessions, even when the service name stays the same.
Checking the Client Tool
In SQL Developer, check the active connection in the Connections panel before running a worksheet. The worksheet is associated with the selected connection, but several worksheets can be open at once. Run the verification query in the worksheet you plan to use.
In SQLcl, the prompt normally shows the connected username, but the prompt is not a complete safety check. A saved connection name can be misleading, and a connection can be changed during a session. Run the query whenever you switch environments.
Reading Connection Problems
If the query runs, authentication and database communication succeeded. If the login fails, the error usually points to a different layer:
ORA-01017usually means the username or password is invalid.ORA-12154usually means Oracle could not resolve the connection alias.ORA-12514usually means the listener does not know the requested service.ORA-12541usually means no listener is reachable at the specified host and port.
These errors occur before the verification query can run. Check the connection identifier, service name, network address, and credentials before troubleshooting SQL.
Exercise
Which query attribute tells you the service that accepted the connection?
Show answer
sys_context('USERENV', 'SERVICE_NAME').