TNS-12571 TNS Packet Writer Failure - Network Write Error Resolution
TNS-12571: TNS Packet Writer Failure
Section titled “TNS-12571: TNS Packet Writer Failure”Error Overview
Section titled “Error Overview”Error Text: TNS-12571: TNS:packet writer failure
This error occurs when the Oracle TNS layer cannot write data packets to the network connection. It indicates a failure in the network communication layer during data transmission, often caused by network interruptions, connection drops, or underlying network infrastructure problems.
Understanding TNS Packet Communication
Section titled “Understanding TNS Packet Communication”TNS Packet Structure
Section titled “TNS Packet Structure”TNS Packet Components├── Packet Header│ ├── Packet Length│ ├── Packet Checksum│ └── Packet Type├── Data Payload│ ├── SQL Statements│ ├── Result Sets│ └── Control Information└── Packet Trailer
Communication Flow
Section titled “Communication Flow”Client → Network → Listener → Database ↓ ↓ ↓ ↓Packet Network Process ResponseWriter Layer Handler Generator
Common Causes
Section titled “Common Causes”Network Infrastructure Issues
Section titled “Network Infrastructure Issues”- Network interface card (NIC) failures
- Network cable disconnections
- Switch or router problems
- Network congestion and packet loss
- MTU (Maximum Transmission Unit) mismatches
Connection State Problems
Section titled “Connection State Problems”# Connection abnormally terminated# Client application crashes# Network timeout exceeded# Firewall dropping packets# TCP window size issues
Oracle Process Issues
Section titled “Oracle Process Issues”- Dedicated server process crashes
- Shared server process problems
- Memory allocation failures
- Resource contention
System Resource Problems
Section titled “System Resource Problems”- Network buffer exhaustion
- System memory shortage
- CPU overload affecting network I/O
- Disk I/O blocking network operations
Diagnostic Steps
Section titled “Diagnostic Steps”Network Layer Analysis
Section titled “Network Layer Analysis”# Check network interface statusifconfig -a # Linux/Unixipconfig /all # Windows
# Check network statisticsnetstat -i # Interface statisticsnetstat -s # Protocol statistics
# Check for packet dropscat /proc/net/dev # Linux# Look for errors, dropped packets
# Check network connectivityping -c 10 database_serverping -s 1500 -c 10 database_server # Large packet test
TNS-Specific Diagnostics
Section titled “TNS-Specific Diagnostics”# Enable TNS tracing# Add to sqlnet.ora:TRACE_LEVEL_CLIENT = 16TRACE_DIRECTORY_CLIENT = /tmp/traceTRACE_FILE_CLIENT = sqlnet_client
# Test connection with tracingsqlplus username/password@service_name
# Analyze trace filesls -la /tmp/trace/grep -i "packet\|write\|error" /tmp/trace/sqlnet_client*.trc
System Resource Monitoring
Section titled “System Resource Monitoring”# Monitor system resources during connection attemptstop -p $(pgrep oracle) # Monitor Oracle processes
# Check memory usagefree -mcat /proc/meminfo
# Monitor network I/Osar -n DEV 1 10 # Network device statisticsiostat -n 1 10 # Network I/O statistics
# Check for system errorsdmesg | grep -i "network\|ethernet\|tcp"
Oracle Database Analysis
Section titled “Oracle Database Analysis”-- Check for network-related wait eventsSELECT event, total_waits, time_waited, average_waitFROM v$system_eventWHERE event LIKE '%TCP%' OR event LIKE '%send%' OR event LIKE '%receive%'ORDER BY time_waited DESC;
-- Check current sessions with network issuesSELECT sid, serial#, username, machine, program, event, wait_class, seconds_in_waitFROM v$sessionWHERE event LIKE '%TCP%' OR event LIKE '%send%' OR event LIKE '%receive%'ORDER BY seconds_in_wait DESC;
-- Check listener connectionsSELECT server, status, COUNT(*) as connection_countFROM v$sessionGROUP BY server, statusORDER BY connection_count DESC;
Resolution Steps
Section titled “Resolution Steps”Network Troubleshooting
Section titled “Network Troubleshooting”1. Basic Network Verification
Section titled “1. Basic Network Verification”# Test basic connectivity with different packet sizesping -s 64 database_server # Small packetsping -s 1472 database_server # Large packets (standard MTU)ping -s 8972 database_server # Very large packets
# Test TCP connectivitytelnet database_server 1521nc -v database_server 1521
# Check route and network pathtraceroute database_servermtr database_server # Better traceroute
2. Network Interface Optimization
Section titled “2. Network Interface Optimization”# Check current network settingsethtool eth0 # Linux - network interface info
# Optimize network buffer sizes# Add to /etc/sysctl.confnet.core.rmem_default = 262144net.core.rmem_max = 4194304net.core.wmem_default = 262144net.core.wmem_max = 4194304net.ipv4.tcp_rmem = 4096 65536 4194304net.ipv4.tcp_wmem = 4096 65536 4194304net.ipv4.tcp_no_metrics_save = 1
# Apply changessysctl -p
3. MTU Discovery and Optimization
Section titled “3. MTU Discovery and Optimization”# Test MTU sizesping -M do -s 1472 database_server # Test standard MTUping -M do -s 1500 database_server # Test larger MTU
# Set optimal MTU if neededifconfig eth0 mtu 1500 # Temporary# Or permanently in network configuration
# Oracle-specific MTU settings in sqlnet.oraSQLNET.RECV_BUF_SIZE = 65536SQLNET.SEND_BUF_SIZE = 65536
Oracle Configuration Fixes
Section titled “Oracle Configuration Fixes”1. Connection Timeout Settings
Section titled “1. Connection Timeout Settings”# Update sqlnet.ora on both client and serverSQLNET.RECV_TIMEOUT = 600SQLNET.SEND_TIMEOUT = 600SQLNET.EXPIRE_TIME = 10TCP.CONNECT_TIMEOUT = 60TCP.NODELAY = yes
# Connection retry settingsSQLNET.RETRY_COUNT = 3SQLNET.RETRY_DELAY = 3
2. Listener Configuration
Section titled “2. Listener Configuration”# Update listener.oraLISTENER = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = server_name)(PORT = 1521)) )
# Connection management settingsRATE_LIMIT_LISTENER = ONCONNECTION_RATE_LISTENER = 50VALID_NODE_CHECKING_REGISTRATION_LISTENER = SUBNET
# Restart listenerlsnrctl stoplsnrctl start
3. Database Parameters
Section titled “3. Database Parameters”-- Optimize network-related database parametersALTER SYSTEM SET processes = 500 SCOPE=BOTH;ALTER SYSTEM SET sessions = 555 SCOPE=BOTH;
-- Connection and memory settingsALTER SYSTEM SET shared_servers = 5 SCOPE=BOTH; -- If using shared serversALTER SYSTEM SET dispatchers = '(PROTOCOL=TCP)(DISPATCHERS=3)' SCOPE=BOTH;
-- Network optimizationALTER SYSTEM SET db_writer_processes = 4 SCOPE=SPFILE; -- Restart required
Advanced Network Troubleshooting
Section titled “Advanced Network Troubleshooting”1. Packet Capture and Analysis
Section titled “1. Packet Capture and Analysis”# Capture Oracle traffictcpdump -i any -w oracle_packets.pcap port 1521
# Capture with detailed infotcpdump -i any -s 0 -A port 1521
# Analyze with Wireshark or tsharktshark -r oracle_packets.pcap -Y "tcp.port==1521"
# Look for retransmissions and errorstshark -r oracle_packets.pcap -Y "tcp.analysis.flags"
2. Network Performance Testing
Section titled “2. Network Performance Testing”# Test network throughputiperf3 -c database_server # Client modeiperf3 -s # Server mode (on database server)
# Test with Oracle-like traffic patternsdd if=/dev/zero bs=8192 count=10000 | nc database_server 1521
3. Connection Pooling Optimization
Section titled “3. Connection Pooling Optimization”// Java connection pool configurationHikariConfig config = new HikariConfig();config.setJdbcUrl("jdbc:oracle:thin:@database_server:1521:ORCL");
// Network timeout settingsconfig.setConnectionTimeout(30000);config.setSocketTimeout(60000);config.setValidationTimeout(5000);
// Connection lifecycle managementconfig.setIdleTimeout(300000); // 5 minutesconfig.setMaxLifetime(1800000); // 30 minutesconfig.setLeakDetectionThreshold(60000); // 60 seconds
// Pool sizingconfig.setMinimumIdle(5);config.setMaximumPoolSize(20);
// Additional Oracle-specific propertiesProperties props = new Properties();props.setProperty("oracle.net.CONNECT_TIMEOUT", "60000");props.setProperty("oracle.jdbc.ReadTimeout", "60000");config.setDataSourceProperties(props);
Application-Level Solutions
Section titled “Application-Level Solutions”Robust Connection Handling
Section titled “Robust Connection Handling”# Python example with retry and error handlingimport cx_Oracleimport timeimport logging
def robust_oracle_connection(retries=3, delay=1): for attempt in range(retries): try: connection = cx_Oracle.connect( user="username", password="password", dsn="database_server:1521/service_name", # Network settings tcp_connect_timeout=60, retry_count=3, retry_delay=1 ) return connection
except cx_Oracle.DatabaseError as e: error_code = e.args[0].code if e.args and hasattr(e.args[0], 'code') else 0
if error_code == 12571: # TNS packet writer failure if attempt < retries - 1: wait_time = delay * (2 ** attempt) # Exponential backoff logging.warning(f"TNS packet writer failure, retrying in {wait_time}s (attempt {attempt + 1})") time.sleep(wait_time) continue raise
raise Exception("Failed to establish connection after all retries")
# Usage with proper cleanupdef execute_query(sql): connection = None try: connection = robust_oracle_connection() cursor = connection.cursor() cursor.execute(sql) return cursor.fetchall() finally: if connection: connection.close()
Connection Health Monitoring
Section titled “Connection Health Monitoring”// Java connection health checkpublic class ConnectionHealthMonitor { private static final String HEALTH_CHECK_SQL = "SELECT 1 FROM DUAL";
public boolean isConnectionHealthy(Connection conn) { try (PreparedStatement stmt = conn.prepareStatement(HEALTH_CHECK_SQL)) { stmt.setQueryTimeout(5); // 5 second timeout ResultSet rs = stmt.executeQuery(); return rs.next() && rs.getInt(1) == 1; } catch (SQLException e) { if (e.getErrorCode() == 12571) { // TNS packet writer failure logNetworkError("Packet writer failure detected", e); } return false; } }
private void logNetworkError(String message, SQLException e) { logger.error("{}: Error code={}, SQL state={}", message, e.getErrorCode(), e.getSQLState(), e); }}
Monitoring and Prevention
Section titled “Monitoring and Prevention”Network Health Monitoring
Section titled “Network Health Monitoring”#!/bin/bash# Network health monitoring for Oracle
LOG_FILE="/var/log/oracle_network_health.log"DB_HOST="database_server"DB_PORT=1521
monitor_network_health() { timestamp=$(date '+%Y-%m-%d %H:%M:%S')
# Test basic connectivity if ! nc -z -w5 $DB_HOST $DB_PORT; then echo "$timestamp: ERROR - Cannot connect to $DB_HOST:$DB_PORT" >> $LOG_FILE return 1 fi
# Test packet transmission with different sizes for size in 64 1472 8192; do if ! ping -c 1 -s $size -W 5 $DB_HOST > /dev/null 2>&1; then echo "$timestamp: WARNING - Packet size $size failed to $DB_HOST" >> $LOG_FILE fi done
# Check network interface statistics rx_errors=$(cat /sys/class/net/eth0/statistics/rx_errors) tx_errors=$(cat /sys/class/net/eth0/statistics/tx_errors)
if [ $rx_errors -gt 0 ] || [ $tx_errors -gt 0 ]; then echo "$timestamp: WARNING - Network errors detected: RX=$rx_errors TX=$tx_errors" >> $LOG_FILE fi
echo "$timestamp: Network health check completed" >> $LOG_FILE}
# Run every 5 minuteswhile true; do monitor_network_health sleep 300done
Oracle Session Monitoring
Section titled “Oracle Session Monitoring”-- Create monitoring procedure for network issuesCREATE OR REPLACE PROCEDURE monitor_network_sessions ASBEGIN -- Log sessions with network wait events INSERT INTO network_session_log SELECT SYSDATE, sid, serial#, username, machine, event, wait_class, seconds_in_wait FROM v$session WHERE event LIKE '%TCP%' OR event LIKE '%send%' OR event LIKE '%receive%' OR event LIKE '%packet%';
-- Alert for long-waiting network sessions FOR rec IN ( SELECT sid, serial#, username, event, seconds_in_wait FROM v$session WHERE (event LIKE '%TCP%' OR event LIKE '%send%' OR event LIKE '%receive%') AND seconds_in_wait > 300 -- 5 minutes ) LOOP DBMS_OUTPUT.PUT_LINE('ALERT: Long network wait - SID: ' || rec.sid || ', Event: ' || rec.event || ', Wait: ' || rec.seconds_in_wait || 's'); END LOOP;
COMMIT;END;/
-- Schedule to run every minuteBEGIN DBMS_SCHEDULER.CREATE_JOB( job_name => 'NETWORK_MONITOR_JOB', job_type => 'PLSQL_BLOCK', job_action => 'BEGIN monitor_network_sessions; END;', start_date => SYSTIMESTAMP, repeat_interval => 'FREQ=MINUTELY;INTERVAL=1', enabled => TRUE );END;/
Automated Recovery Scripts
Section titled “Automated Recovery Scripts”#!/bin/bash# Automated recovery for network issues
ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1export ORACLE_HOME
detect_network_issues() { # Check for TNS-12571 errors in alert log ERROR_COUNT=$(tail -1000 $ORACLE_BASE/diag/rdbms/$ORACLE_SID/$ORACLE_SID/trace/alert_$ORACLE_SID.log | \ grep -c "TNS-12571")
if [ $ERROR_COUNT -gt 5 ]; then echo "High TNS-12571 error count detected: $ERROR_COUNT" return 1 fi
return 0}
recovery_actions() { echo "Performing network recovery actions..."
# Restart network interface (if allowed) # sudo ifdown eth0 && sudo ifup eth0
# Clear network buffers echo 3 > /proc/sys/vm/drop_caches
# Restart listener lsnrctl stop sleep 5 lsnrctl start
# Force service registration $ORACLE_HOME/bin/sqlplus -s / as sysdba << EOF ALTER SYSTEM REGISTER; EXIT;EOF
echo "Recovery actions completed"}
# Main monitoring loopif ! detect_network_issues; then recovery_actions
# Send notification echo "Oracle network recovery performed due to TNS-12571 errors" | \fi
Performance Optimization
Section titled “Performance Optimization”Network Buffer Tuning
Section titled “Network Buffer Tuning”# System-level network optimization# Add to /etc/sysctl.conf
# Increase network buffer sizesnet.core.rmem_default = 1048576net.core.rmem_max = 16777216net.core.wmem_default = 1048576net.core.wmem_max = 16777216
# TCP buffer sizesnet.ipv4.tcp_rmem = 4096 1048576 16777216net.ipv4.tcp_wmem = 4096 1048576 16777216
# Reduce TCP TIME_WAIT socketsnet.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_fin_timeout = 30
# Apply settingssysctl -p
Oracle Network Parameters
Section titled “Oracle Network Parameters”-- Database network optimizationALTER SYSTEM SET shared_pool_size = 256M SCOPE=BOTH;ALTER SYSTEM SET large_pool_size = 64M SCOPE=BOTH;
-- Connection managementALTER SYSTEM SET processes = 1000 SCOPE=SPFILE; -- Restart requiredALTER SYSTEM SET sessions = 1105 SCOPE=SPFILE; -- Restart required
-- Network-specific parametersALTER SYSTEM SET tcp_invited_nodes = 'ALL' SCOPE=BOTH;ALTER SYSTEM SET sql_trace = FALSE SCOPE=BOTH; -- Reduce overhead if enabled
Related Errors
Section titled “Related Errors”- TNS-12150: TNS unable to send data
- TNS-12152: TNS unable to send break message
- TNS-12535: TNS operation timed out
- ORA-03113: End-of-file on communication channel
Best Practices
Section titled “Best Practices”- Implement robust connection retry logic
- Monitor network health continuously
- Optimize network buffer sizes
- Use connection pooling effectively
- Regular network infrastructure maintenance
- Implement proper timeout settings
Troubleshooting Checklist
Section titled “Troubleshooting Checklist”- Test basic network connectivity
- Check network interface statistics
- Verify MTU settings and packet sizes
- Analyze TNS trace files
- Monitor system resources
- Check Oracle listener status
- Implement connection retry logic
- Optimize network buffer settings