1 25 package com.mysql.jdbc; 26 27 import java.net.BindException ; 28 29 import java.sql.SQLException ; 30 31 43 public class CommunicationsException extends SQLException { 44 45 private static final long DEFAULT_WAIT_TIMEOUT_SECONDS = 28800; 46 47 private static final int DUE_TO_TIMEOUT_FALSE = 0; 48 49 private static final int DUE_TO_TIMEOUT_MAYBE = 2; 50 51 private static final int DUE_TO_TIMEOUT_TRUE = 1; 52 53 private String exceptionMessage; 54 55 public CommunicationsException(Connection conn, long lastPacketSentTimeMs, 56 Exception underlyingException) { 57 58 long serverTimeoutSeconds = 0; 59 boolean isInteractiveClient = false; 60 61 if (conn != null) { 62 isInteractiveClient = conn.getInteractiveClient(); 63 64 String serverTimeoutSecondsStr = null; 65 66 if (isInteractiveClient) { 67 serverTimeoutSecondsStr = conn 68 .getServerVariable("interactive_timeout"); } else { 70 serverTimeoutSecondsStr = conn 71 .getServerVariable("wait_timeout"); } 73 74 if (serverTimeoutSecondsStr != null) { 75 try { 76 serverTimeoutSeconds = Long 77 .parseLong(serverTimeoutSecondsStr); 78 } catch (NumberFormatException nfe) { 79 serverTimeoutSeconds = 0; 80 } 81 } 82 } 83 84 StringBuffer exceptionMessageBuf = new StringBuffer (); 85 86 if (lastPacketSentTimeMs == 0) { 87 lastPacketSentTimeMs = System.currentTimeMillis(); 88 } 89 90 long timeSinceLastPacket = (System.currentTimeMillis() - lastPacketSentTimeMs) / 1000; 91 92 int dueToTimeout = DUE_TO_TIMEOUT_FALSE; 93 94 StringBuffer timeoutMessageBuf = null; 95 96 if (serverTimeoutSeconds != 0) { 97 if (timeSinceLastPacket > serverTimeoutSeconds) { 98 dueToTimeout = DUE_TO_TIMEOUT_TRUE; 99 100 timeoutMessageBuf = new StringBuffer (); 101 102 timeoutMessageBuf.append(Messages 103 .getString("CommunicationsException.2")); 105 if (!isInteractiveClient) { 106 timeoutMessageBuf.append(Messages 107 .getString("CommunicationsException.3")); } else { 109 timeoutMessageBuf.append(Messages 110 .getString("CommunicationsException.4")); } 112 113 } 114 } else if (timeSinceLastPacket > DEFAULT_WAIT_TIMEOUT_SECONDS) { 115 dueToTimeout = DUE_TO_TIMEOUT_MAYBE; 116 117 timeoutMessageBuf = new StringBuffer (); 118 119 timeoutMessageBuf.append(Messages 120 .getString("CommunicationsException.5")); timeoutMessageBuf.append(Messages 122 .getString("CommunicationsException.6")); timeoutMessageBuf.append(Messages 124 .getString("CommunicationsException.7")); timeoutMessageBuf.append(Messages 126 .getString("CommunicationsException.8")); } 128 129 if (dueToTimeout == DUE_TO_TIMEOUT_TRUE 130 || dueToTimeout == DUE_TO_TIMEOUT_MAYBE) { 131 132 exceptionMessageBuf.append(Messages 133 .getString("CommunicationsException.9")); exceptionMessageBuf.append(timeSinceLastPacket); 135 exceptionMessageBuf.append(Messages 136 .getString("CommunicationsException.10")); 138 if (timeoutMessageBuf != null) { 139 exceptionMessageBuf.append(timeoutMessageBuf); 140 } 141 142 exceptionMessageBuf.append(Messages 143 .getString("CommunicationsException.11")); exceptionMessageBuf.append(Messages 145 .getString("CommunicationsException.12")); exceptionMessageBuf.append(Messages 147 .getString("CommunicationsException.13")); 149 } else { 150 155 if (underlyingException instanceof BindException ) { 156 exceptionMessageBuf.append(Messages 158 .getString("CommunicationsException.14")); exceptionMessageBuf.append(Messages 160 .getString("CommunicationsException.15")); exceptionMessageBuf.append(Messages 162 .getString("CommunicationsException.16")); exceptionMessageBuf.append(Messages 164 .getString("CommunicationsException.17")); exceptionMessageBuf.append(Messages 166 .getString("CommunicationsException.18")); exceptionMessageBuf.append(Messages 168 .getString("CommunicationsException.19")); } 170 } 171 172 if (exceptionMessageBuf.length() == 0) { 173 exceptionMessageBuf.append(Messages 175 .getString("CommunicationsException.20")); 177 if (underlyingException != null) { 178 exceptionMessageBuf.append(Messages 179 .getString("CommunicationsException.21")); exceptionMessageBuf.append(Util 181 .stackTraceToString(underlyingException)); 182 } 183 184 if (conn != null && conn.getMaintainTimeStats() && 185 !conn.getParanoid()) { 186 exceptionMessageBuf.append("\n\nLast packet sent to the server was "); 187 exceptionMessageBuf.append(System.currentTimeMillis() - lastPacketSentTimeMs); 188 exceptionMessageBuf.append(" ms ago."); 189 } 190 } 191 192 this.exceptionMessage = exceptionMessageBuf.toString(); 193 } 194 195 200 public String getMessage() { 201 return this.exceptionMessage; 202 } 203 204 209 public String getSQLState() { 210 return SQLError.SQL_STATE_COMMUNICATION_LINK_FAILURE; 211 } 212 213 } 214 | Popular Tags |