public static String createLinkFailureMessageBasedOnHeuristics(MySQLConnection conn,long lastPacketSentTimeMs,long lastPacketReceivedTimeMs,Exception underlyingException){
  long serverTimeoutSeconds=0;
  boolean isInteractiveClient=false;
  if (conn != null) {
    isInteractiveClient=conn.getInteractiveClient();
    String serverTimeoutSecondsStr=null;
    if (isInteractiveClient) {
      serverTimeoutSecondsStr=conn.getServerVariable("interactive_timeout");
    }
 else {
      serverTimeoutSecondsStr=conn.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"));
      if (!isInteractiveClient) {
        timeoutMessageBuf.append(Messages.getString("CommunicationsException.3"));
      }
 else {
        timeoutMessageBuf.append(Messages.getString("CommunicationsException.4"));
      }
    }
  }
 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) {
    if (lastPacketReceivedTimeMs != 0) {
      Object[] timingInfo={Long.valueOf(timeSinceLastPacketReceivedMs),Long.valueOf(timeSinceLastPacketSentMs)};
      exceptionMessageBuf.append(Messages.getString("CommunicationsException.ServerPacketTimingInfo",timingInfo));
    }
 else {
      exceptionMessageBuf.append(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 {
    if (underlyingException instanceof BindException) {
      if (conn.getLocalSocketAddress() != null && !Util.interfaceExists(conn.getLocalSocketAddress())) {
        exceptionMessageBuf.append(Messages.getString("CommunicationsException.LocalSocketAddressNotAvailable"));
      }
 else {
        exceptionMessageBuf.append(Messages.getString("CommunicationsException.TooManyClientConnections"));
      }
    }
  }
  if (exceptionMessageBuf.length() == 0) {
    exceptionMessageBuf.append(Messages.getString("CommunicationsException.20"));
    if (conn != null && conn.getMaintainTimeStats() && !conn.getParanoid()) {
      exceptionMessageBuf.append("\n\n");
      if (lastPacketReceivedTimeMs != 0) {
        Object[] timingInfo={Long.valueOf(timeSinceLastPacketReceivedMs),Long.valueOf(timeSinceLastPacketSentMs)};
        exceptionMessageBuf.append(Messages.getString("CommunicationsException.ServerPacketTimingInfo",timingInfo));
      }
 else {
        exceptionMessageBuf.append(Messages.getString("CommunicationsException.ServerPacketTimingInfoNoRecv",new Object[]{Long.valueOf(timeSinceLastPacketSentMs)}));
      }
    }
  }
  return exceptionMessageBuf.toString();
}

public static String createLinkFailureMessageBasedOnHeuristics ( MySQLConnection conn , long lastPacketSentTimeMs , long lastPacketReceivedTimeMs , Exception underlyingException ) { long serverTimeoutSeconds = 0 ; boolean isInteractiveClient = false ; if ( conn != null ) { isInteractiveClient = conn . getInteractiveClient ( ) ; String serverTimeoutSecondsStr = null ; if ( isInteractiveClient ) { serverTimeoutSecondsStr = conn . getServerVariable ( "interactive_timeout" ) ; } else { serverTimeoutSecondsStr = conn . 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" ) ) ; if ( ! isInteractiveClient ) { timeoutMessageBuf . append ( Messages . getString ( "CommunicationsException.3" ) ) ; } else { timeoutMessageBuf . append ( Messages . getString ( "CommunicationsException.4" ) ) ; } } } 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 ) { if ( lastPacketReceivedTimeMs != 0 ) { Object [ ] timingInfo = { Long . valueOf ( timeSinceLastPacketReceivedMs ) , Long . valueOf ( timeSinceLastPacketSentMs ) } ; exceptionMessageBuf . append ( Messages . getString ( "CommunicationsException.ServerPacketTimingInfo" , timingInfo ) ) ; } else { exceptionMessageBuf . append ( 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 { if ( underlyingException instanceof BindException ) { if ( conn . getLocalSocketAddress ( ) != null && ! Util . interfaceExists ( conn . getLocalSocketAddress ( ) ) ) { exceptionMessageBuf . append ( Messages . getString ( "CommunicationsException.LocalSocketAddressNotAvailable" ) ) ; } else { exceptionMessageBuf . append ( Messages . getString ( "CommunicationsException.TooManyClientConnections" ) ) ; } } } if ( exceptionMessageBuf . length ( ) == 0 ) { exceptionMessageBuf . append ( Messages . getString ( "CommunicationsException.20" ) ) ; if ( conn != null && conn . getMaintainTimeStats ( ) && ! conn . getParanoid ( ) ) { exceptionMessageBuf . append ( "\n\n" ) ; if ( lastPacketReceivedTimeMs != 0 ) { Object [ ] timingInfo = { Long . valueOf ( timeSinceLastPacketReceivedMs ) , Long . valueOf ( timeSinceLastPacketSentMs ) } ; exceptionMessageBuf . append ( Messages . getString ( "CommunicationsException.ServerPacketTimingInfo" , timingInfo ) ) ; } else { exceptionMessageBuf . append ( Messages . getString ( "CommunicationsException.ServerPacketTimingInfoNoRecv" , new Object [ ] { Long . valueOf ( timeSinceLastPacketSentMs ) } ) ) ; } } } return exceptionMessageBuf . toString ( ) ; }
creates a communications link failure message to be used in communicationsexception that ( hopefully ) has some better information and suggestions based on heuristics .
