ORA-12547 - TNS Lost Contact
ORA-12547: TNS Lost Contact
Section titled “ORA-12547: TNS Lost Contact”The ORA-12547 error occurs when the Oracle client loses network contact with the Oracle server during an active database connection. This error indicates a communication breakdown between the client and server processes.
Error Details
Section titled “Error Details”- Error Code: ORA-12547
- Error Message: “TNS:lost contact”
- Error Type: Network Communication Error
- Severity: Connection Error
Common Causes
Section titled “Common Causes”1. Network Connectivity Issues
Section titled “1. Network Connectivity Issues”- Network cable disconnection
- Router or switch failures
- Network congestion or packet loss
- Firewall blocking connections
2. Oracle Listener Problems
Section titled “2. Oracle Listener Problems”- Listener service stopped or crashed
- Listener configuration errors
- Listener running out of processes
3. Server-Side Issues
Section titled “3. Server-Side Issues”- Database instance shutdown
- Server hardware failure
- Operating system crashes
- Memory or resource exhaustion
4. Client-Side Issues
Section titled “4. Client-Side Issues”- Client application crashes
- Client machine network problems
- Timeout settings too aggressive
- Client TNS configuration errors
5. Long-Running Operations
Section titled “5. Long-Running Operations”- Query or transaction timeouts
- Idle connection timeouts
- Network timeout configurations
Examples and Solutions
Section titled “Examples and Solutions”Example 1: Network Connectivity Test
Section titled “Example 1: Network Connectivity Test”Problem:
SQL> SELECT * FROM employees;-- Connection works initially
SQL> SELECT COUNT(*) FROM large_table;-- ORA-12547: TNS:lost contact
Solution:
# Test basic network connectivityping database_server
# Test TNS connectivitytnsping SERVICE_NAME
# Check if listener is runninglsnrctl status
# Test telnet to listener porttelnet database_server 1521
Example 2: Listener Issues
Section titled “Example 2: Listener Issues”Problem:
$ sqlplus user/password@database-- ORA-12547: TNS:lost contact
Solution:
# Check listener statuslsnrctl status
# If listener is down, start itlsnrctl start
# Reload listener configurationlsnrctl reload
# Check listener log for errorstail -f $ORACLE_BASE/diag/tnslsnr/hostname/listener/trace/listener.log
Example 3: Connection Timeout
Section titled “Example 3: Connection Timeout”Problem:
-- Long-running query loses connectionSQL> SELECT /*+ FULL(t) */ * FROM huge_table t WHERE complex_condition = 'value';-- ORA-12547: TNS:lost contact (after several minutes)
Solution:
-- Increase timeout values in sqlnet.ora-- On client side:SQLNET.RECV_TIMEOUT=600SQLNET.SEND_TIMEOUT=600SQLNET.INBOUND_CONNECT_TIMEOUT=600SQLNET.OUTBOUND_CONNECT_TIMEOUT=120
-- On server side (listener.ora):INBOUND_CONNECT_TIMEOUT_LISTENER=600
-- Or break query into smaller chunksSELECT COUNT(*) FROM huge_tableWHERE complex_condition = 'value'AND ROWNUM <= 10000;
Example 4: Firewall Configuration
Section titled “Example 4: Firewall Configuration”Problem:
# Connection works initially but drops during operation$ sqlplus user/password@databaseSQL> -- Works for simple queriesSQL> -- Fails on complex operations-- ORA-12547: TNS:lost contact
Solution:
# Check firewall rules# Linux iptablesiptables -L -n | grep 1521
# Windows firewallnetsh advfirewall firewall show rule name="Oracle"
# Add firewall rules for Oracle ports# Linux (allow Oracle listener)iptables -A INPUT -p tcp --dport 1521 -j ACCEPT
# Configure TNS for dedicated connections# In listener.ora:SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (ORACLE_HOME = /u01/app/oracle/product/19.0.0/dbhome_1) (SID_NAME = ORCL) ) )
Example 5: Resource Exhaustion
Section titled “Example 5: Resource Exhaustion”Problem:
-- Multiple connections start failingSQL> CONNECT user/password@database-- ORA-12547: TNS:lost contact
Solution:
-- Check database parametersSELECT name, value FROM v$parameterWHERE name IN ('processes', 'sessions', 'shared_servers');
-- Check current usageSELECT 'Sessions' as resource, COUNT(*) as current_usage, (SELECT value FROM v$parameter WHERE name = 'sessions') as limitFROM v$sessionUNION ALLSELECT 'Processes' as resource, COUNT(*) as current_usage, (SELECT value FROM v$parameter WHERE name = 'processes') as limitFROM v$process;
-- Increase limits if necessary (requires restart)ALTER SYSTEM SET processes=500 SCOPE=SPFILE;ALTER SYSTEM SET sessions=800 SCOPE=SPFILE;
Diagnostic Queries
Section titled “Diagnostic Queries”Check Current Connections
Section titled “Check Current Connections”-- Check active sessionsSELECT username, program, machine, status, logon_time, last_call_etFROM v$sessionWHERE username IS NOT NULLORDER BY last_call_et DESC;
-- Check connection counts by machineSELECT machine, COUNT(*) as connection_countFROM v$sessionWHERE username IS NOT NULLGROUP BY machineORDER BY connection_count DESC;
Monitor Listener Activity
Section titled “Monitor Listener Activity”-- Check listener endpointsSELECT name, valueFROM v$parameterWHERE name LIKE '%listener%'OR name LIKE '%dispatch%';
-- Check TNS entriesSELECT network_service_bannerFROM v$session_connect_infoWHERE sid = (SELECT sid FROM v$mystat WHERE rownum = 1);
Network Diagnostics
Section titled “Network Diagnostics”# Check network statisticsnetstat -an | grep :1521
# Monitor network traffic# Linuxss -tuln | grep :1521
# Check for dropped packetsnetstat -i
# Monitor connection attempts# Check listener logtail -f $ORACLE_BASE/diag/tnslsnr/*/listener/trace/listener.log
Resolution Steps
Section titled “Resolution Steps”Step 1: Immediate Response
Section titled “Step 1: Immediate Response”# Test basic connectivityping database_server
# Test TNS resolutiontnsping SERVICE_NAME
# Check listener statuslsnrctl status LISTENER_NAME
Step 2: Check Logs
Section titled “Step 2: Check Logs”# Check Oracle alert logtail -f $ORACLE_BASE/diag/rdbms/*/trace/alert_*.log
# Check listener logtail -f $ORACLE_BASE/diag/tnslsnr/*/listener/trace/listener.log
# Check system logs# Linuxjournalctl -u oracle-database# Ortail -f /var/log/messages
Step 3: Network Troubleshooting
Section titled “Step 3: Network Troubleshooting”# Check network pathtraceroute database_server
# Test specific porttelnet database_server 1521
# Check for packet lossping -c 100 database_server | tail -5
Step 4: Configuration Verification
Section titled “Step 4: Configuration Verification”# Verify TNS configurationcat $ORACLE_HOME/network/admin/tnsnames.ora
# Check sqlnet.ora settingscat $ORACLE_HOME/network/admin/sqlnet.ora
# Verify listener configurationcat $ORACLE_HOME/network/admin/listener.ora
Prevention Strategies
Section titled “Prevention Strategies”1. Network Monitoring
Section titled “1. Network Monitoring”# Set up network monitoring# Monitor packet loss, latency, bandwidth# Alert on network issues
# Example monitoring script#!/bin/bashwhile true; do if ! ping -c 1 database_server >/dev/null 2>&1; then echo "$(date): Network connectivity lost to database_server" >> /var/log/oracle_network.log fi sleep 60done
2. Connection Pooling
Section titled “2. Connection Pooling”// Use connection pooling to handle transient network issues// Example with Oracle UCPPoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();pds.setConnectionPoolName("MyPool");pds.setInitialPoolSize(5);pds.setMinPoolSize(5);pds.setMaxPoolSize(20);pds.setTimeoutCheckInterval(5);pds.setInactiveConnectionTimeout(300);
3. Timeout Configuration
Section titled “3. Timeout Configuration”# Configure appropriate timeouts in sqlnet.oraSQLNET.RECV_TIMEOUT=600SQLNET.SEND_TIMEOUT=600SQLNET.INBOUND_CONNECT_TIMEOUT=600SQLNET.OUTBOUND_CONNECT_TIMEOUT=120SQLNET.EXPIRE_TIME=10
# TCP keepalive settingsSQLNET.TCP.NODELAY=YES
4. Health Checks
Section titled “4. Health Checks”-- Implement regular health checksCREATE OR REPLACE PROCEDURE check_database_health IS v_count NUMBER;BEGIN SELECT COUNT(*) INTO v_count FROM dual; DBMS_OUTPUT.PUT_LINE('Database health check: OK at ' || SYSTIMESTAMP);EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Database health check failed: ' || SQLERRM);END;/
Monitoring and Alerting
Section titled “Monitoring and Alerting”Real-time Monitoring
Section titled “Real-time Monitoring”-- Monitor for connection issuesSELECT TO_CHAR(first_time, 'YYYY-MM-DD HH24:MI:SS') as error_time, message_textFROM v$diag_alert_extWHERE message_text LIKE '%TNS%'OR message_text LIKE '%lost contact%'ORDER BY first_time DESC;
Connection Health Script
Section titled “Connection Health Script”#!/bin/bash# Oracle connection health checkORACLE_SID=ORCLSERVICE_NAME=mydb.example.com
# Test TNS pingif tnsping $SERVICE_NAME | grep -q "OK"; then echo "TNS ping successful"else echo "TNS ping failed - check listener" exit 1fi
# Test SQL connectionif echo "SELECT 1 FROM dual;" | sqlplus -s system/password@$SERVICE_NAME | grep -q "1"; then echo "SQL connection successful"else echo "SQL connection failed" exit 1fi
Related Errors
Section titled “Related Errors”- ORA-12541: TNS No Listener
- ORA-12514: TNS Service Registration
- ORA-03113: End-of-file on Communication
- TNS-12571: TNS Packet Writer Failure
Summary
Section titled “Summary”ORA-12547 indicates a network communication failure between Oracle client and server. Resolution involves diagnosing network connectivity, checking listener status, verifying configurations, and implementing proper timeout settings. Prevention requires robust network monitoring, connection pooling, and appropriate timeout configurations to handle transient network issues gracefully.