in src/main/core-api/java/com/mysql/cj/exceptions/ExceptionFactory.java [187:299]
public static String createLinkFailureMessageBasedOnHeuristics(PropertySet propertySet, ServerSession serverSession,
PacketSentTimeHolder packetSentTimeHolder, PacketReceivedTimeHolder packetReceivedTimeHolder, Throwable underlyingException) {
long serverTimeoutSeconds = 0;
boolean isInteractiveClient = false;
long lastPacketReceivedTimeMs = packetReceivedTimeHolder == null ? 0L : packetReceivedTimeHolder.getLastPacketReceivedTime();
long lastPacketSentTimeMs = packetSentTimeHolder.getLastPacketSentTime();
if (lastPacketSentTimeMs > lastPacketReceivedTimeMs) {
lastPacketSentTimeMs = packetSentTimeHolder.getPreviousPacketSentTime();
}
if (propertySet != null) {
isInteractiveClient = propertySet.getBooleanProperty(PropertyKey.interactiveClient).getValue();
String serverTimeoutSecondsStr = null;
if (serverSession != null) {
serverTimeoutSecondsStr = isInteractiveClient ? serverSession.getServerVariable("interactive_timeout")
: serverSession.getServerVariable("wait_timeout");
}
if (serverTimeoutSecondsStr != null) {
try {
serverTimeoutSeconds = Long.parseLong(serverTimeoutSecondsStr);
} catch (NumberFormatException nfe) {
serverTimeoutSeconds = 0;
}
}
}
StringBuilder exceptionMessageBuf = new StringBuilder();
long nowMs = System.currentTimeMillis();
if (lastPacketSentTimeMs == 0) {
lastPacketSentTimeMs = nowMs;
}
long timeSinceLastPacketSentMs = nowMs - lastPacketSentTimeMs;
long timeSinceLastPacketSeconds = timeSinceLastPacketSentMs / 1000;
long timeSinceLastPacketReceivedMs = nowMs - lastPacketReceivedTimeMs;
int dueToTimeout = DUE_TO_TIMEOUT_FALSE;
StringBuilder timeoutMessageBuf = null;
if (serverTimeoutSeconds != 0) {
if (timeSinceLastPacketSeconds > serverTimeoutSeconds) {
dueToTimeout = DUE_TO_TIMEOUT_TRUE;
timeoutMessageBuf = new StringBuilder();
timeoutMessageBuf.append(Messages.getString("CommunicationsException.2"));
timeoutMessageBuf.append(Messages.getString(isInteractiveClient ? "CommunicationsException.4" : "CommunicationsException.3"));
}
} else if (timeSinceLastPacketSeconds > DEFAULT_WAIT_TIMEOUT_SECONDS) {
dueToTimeout = DUE_TO_TIMEOUT_MAYBE;
timeoutMessageBuf = new StringBuilder();
timeoutMessageBuf.append(Messages.getString("CommunicationsException.5"));
timeoutMessageBuf.append(Messages.getString("CommunicationsException.6"));
timeoutMessageBuf.append(Messages.getString("CommunicationsException.7"));
timeoutMessageBuf.append(Messages.getString("CommunicationsException.8"));
}
if (dueToTimeout == DUE_TO_TIMEOUT_TRUE || dueToTimeout == DUE_TO_TIMEOUT_MAYBE) {
exceptionMessageBuf.append(lastPacketReceivedTimeMs != 0
? Messages.getString("CommunicationsException.ServerPacketTimingInfo",
new Object[] { Long.valueOf(timeSinceLastPacketReceivedMs), Long.valueOf(timeSinceLastPacketSentMs) })
: Messages.getString("CommunicationsException.ServerPacketTimingInfoNoRecv", new Object[] { Long.valueOf(timeSinceLastPacketSentMs) }));
if (timeoutMessageBuf != null) {
exceptionMessageBuf.append(timeoutMessageBuf);
}
exceptionMessageBuf.append(Messages.getString("CommunicationsException.11"));
exceptionMessageBuf.append(Messages.getString("CommunicationsException.12"));
exceptionMessageBuf.append(Messages.getString("CommunicationsException.13"));
} else {
//
// Attempt to determine the reason for the underlying exception (we can only make a best-guess here)
//
if (underlyingException instanceof BindException) {
String localSocketAddress = propertySet.getStringProperty(PropertyKey.localSocketAddress).getValue();
boolean interfaceNotAvaliable;
try {
interfaceNotAvaliable = localSocketAddress != null && NetworkInterface.getByName(localSocketAddress) == null;
} catch (SocketException e1) {
interfaceNotAvaliable = false;
}
exceptionMessageBuf.append(interfaceNotAvaliable ? Messages.getString("CommunicationsException.LocalSocketAddressNotAvailable")
: Messages.getString("CommunicationsException.TooManyClientConnections"));
}
}
if (exceptionMessageBuf.length() == 0) {
// We haven't figured out a good reason, so copy it.
exceptionMessageBuf.append(Messages.getString("CommunicationsException.20"));
if (propertySet.getBooleanProperty(PropertyKey.maintainTimeStats).getValue() && !propertySet.getBooleanProperty(PropertyKey.paranoid).getValue()) {
exceptionMessageBuf.append("\n\n");
exceptionMessageBuf.append(lastPacketReceivedTimeMs != 0
? Messages.getString("CommunicationsException.ServerPacketTimingInfo",
new Object[] { Long.valueOf(timeSinceLastPacketReceivedMs), Long.valueOf(timeSinceLastPacketSentMs) })
: Messages.getString("CommunicationsException.ServerPacketTimingInfoNoRecv", new Object[] { Long.valueOf(timeSinceLastPacketSentMs) }));
}
}
return exceptionMessageBuf.toString();
}