Database Links Analysis (ddblink.sql)
What This Script Does
Section titled “What This Script Does”This script displays database links configuration showing ownership, connection details, and target database information. It helps DBAs manage distributed database connections, troubleshoot connectivity issues, and audit database link usage across schemas.
Script
Section titled “Script”rem ddblink.sqlremrem linesize = 80remttitle 'Database Links'remcol owner format a12 heading 'OWNER'col db_link format a15 heading 'DATABASE LINK'col username format a12 heading 'USERNAME'col password format a10 heading 'PASSWORD'col host format a17 heading 'HOST'col created format a9 heading 'CREATED'rembreak on owner skip 1remselect owner, db_link, username, host, created from sys.dba_db_links where owner like upper('&owner') order by owner, db_link;
-- Run the script in SQL*Plus or SQLcl@ddblink.sql
-- When prompted, enter:-- owner: Schema owner pattern (% for all)
-- ExamplesEnter value for owner: % -- Show all database linksEnter value for owner: HR -- Show links owned by HR schema
Parameters
Section titled “Parameters”- &owner: Schema owner pattern (use % for all schemas)
Required Privileges
Section titled “Required Privileges”- SELECT on SYS.DBA_DB_LINKS
- Typically requires DBA role
Sample Output
Section titled “Sample Output”Database Links
OWNER DATABASE LINK USERNAME HOST CREATED------------ --------------- ------------ ---------------- ---------HR REMOTE_HR HR_USER remote-db.com 15-MAR-24 BACKUP_DB BACKUP_USER backup-srv.com 20-MAR-24
PUBLIC SHARED_LINK APP_USER shared-db.com 01-JAN-24 REPORTING REPORT_USER report-db.com 15-FEB-24
SALES SALES_DW DW_USER warehouse.com 10-APR-24
Key Output Columns
Section titled “Key Output Columns”- OWNER: Schema owner of the database link
- DATABASE LINK: Name of the database link
- USERNAME: Username for remote database connection
- HOST: Target database host or connect string
- CREATED: Date when the link was created
Understanding Database Links
Section titled “Understanding Database Links”Database Link Types
Section titled “Database Link Types”- Private Links: Owned by specific schema (HR, SALES, etc.)
- Public Links: Available to all users (OWNER = ‘PUBLIC’)
- Global Links: Managed by Oracle Names or LDAP
Security Considerations
Section titled “Security Considerations”- Fixed User Links: Use specified username/password
- Connected User Links: Use current session credentials
- Shared Links: Multiple users share same connection
Performance Analysis
Section titled “Performance Analysis”Link Usage Monitoring
Section titled “Link Usage Monitoring”-- Check active database link sessionsSELECT db_link, in_transaction, opened, logged_on, heterogeneous, protocolFROM v$dblink;
Connection Pool Analysis
Section titled “Connection Pool Analysis”-- Monitor database link performanceSELECT owner, db_link_name, open_connections, in_transactionFROM dba_db_link_sourcesWHERE open_connections > 0;
Common Use Cases
Section titled “Common Use Cases”Database Link Inventory
Section titled “Database Link Inventory”-- List all database links in the system@ddblink.sql-- Enter % for owner-- Review all configured links
Security Audit
Section titled “Security Audit”-- Check for shared or public links@ddblink.sql-- Look for PUBLIC owner-- Review username patterns
Troubleshooting Connectivity
Section titled “Troubleshooting Connectivity”-- Identify problematic links@ddblink.sql-- Test connectivity for each link-- Check host reachability
Troubleshooting Database Links
Section titled “Troubleshooting Database Links”Test Link Connectivity
Section titled “Test Link Connectivity”-- Test database link functionalitySELECT COUNT(*) FROM dual@REMOTE_HR;
-- Check remote database versionSELECT banner FROM v$version@REMOTE_HR WHERE rownum = 1;
Connection Issues
Section titled “Connection Issues”-- Verify link configurationSELECT owner, db_link, username, hostFROM dba_db_linksWHERE db_link = 'REMOTE_HR';
-- Check TNS configurationSELECT db_link, hostFROM dba_db_linksWHERE host LIKE '%tns_name%';
Performance Problems
Section titled “Performance Problems”-- Monitor link response timeSELECT db_link, COUNT(*) connections, AVG(open_cursors) avg_cursorsFROM v$dblinkGROUP BY db_link;
Advanced Analysis
Section titled “Advanced Analysis”Link Dependencies
Section titled “Link Dependencies”-- Find objects using database linksSELECT owner, name, type, referenced_owner, referenced_nameFROM dba_dependenciesWHERE referenced_link_name IS NOT NULLORDER BY owner, name;
Cross-Database Queries
Section titled “Cross-Database Queries”-- Identify views using database linksSELECT owner, view_name, textFROM dba_viewsWHERE text LIKE '%@%'ORDER BY owner, view_name;
Security Analysis
Section titled “Security Analysis”-- Check for links with stored passwordsSELECT owner, db_link, CASE WHEN username IS NOT NULL THEN 'Fixed User' ELSE 'Connected User' END link_type, hostFROM dba_db_linksORDER BY link_type, owner;
Link Usage Statistics
Section titled “Link Usage Statistics”-- Historical link usage (if auditing enabled)SELECT db_link, COUNT(*) access_count, MIN(timestamp) first_access, MAX(timestamp) last_accessFROM dba_audit_trailWHERE obj_name LIKE '%@%'GROUP BY db_linkORDER BY access_count DESC;
Database Link Management
Section titled “Database Link Management”Creating Database Links
Section titled “Creating Database Links”-- Create private database linkCREATE DATABASE LINK remote_hrCONNECT TO hr_user IDENTIFIED BY passwordUSING 'remote_db_tns';
-- Create public database linkCREATE PUBLIC DATABASE LINK shared_linkCONNECT TO app_user IDENTIFIED BY passwordUSING 'shared_db_tns';
-- Create connected user linkCREATE DATABASE LINK current_user_linkCONNECT TO CURRENT_USERUSING 'target_db_tns';
Maintenance Operations
Section titled “Maintenance Operations”-- Drop database linkDROP DATABASE LINK remote_hr;
-- Drop public database linkDROP PUBLIC DATABASE LINK shared_link;
Security Best Practices
Section titled “Security Best Practices”Password Management
Section titled “Password Management”- Avoid storing passwords in database links when possible
- Use connected user links with proper authentication
- Regularly rotate passwords for fixed user links
- Consider using Oracle Wallet for credential storage
Access Control
Section titled “Access Control”-- Grant database link usageGRANT CREATE DATABASE LINK TO schema_owner;
-- Revoke database link privilegesREVOKE CREATE DATABASE LINK FROM schema_owner;
Monitoring
Section titled “Monitoring”-- Regular security auditSELECT owner, db_link, username, CASE WHEN username IS NULL THEN 'SECURE' ELSE 'REVIEW' END security_statusFROM dba_db_linksORDER BY security_status DESC, owner;
Related Scripts
Section titled “Related Scripts”- Database Information (../database-info/db.sql) - Database overview
- User Analysis (../security-analysis/duser.sql) - User account management
- Network Configuration (../administration/vcontrol.sql) - Database configuration
- Dependencies Analysis (dobject.sql) - Object dependencies