Пример #1
0
  /**
   * Establishes the TCP/IP connection to serverName and portNumber of this Connection
   *
   * @throws SQLTimeoutException If the connection cannot be established within the connect timeout
   *     (either explicitly set or implied by the OS timeout of the socket)
   * @throws SQLException If the connection cannot be established.
   */
  public final void socketConnect() throws SQLException {
    try {
      socket = new Socket();
      socket.setTcpNoDelay(true);
      final int connectTimeout = attachProperties.getConnectTimeout();
      final int socketConnectTimeout;
      if (connectTimeout != -1) {
        // connectTimeout is in seconds, need milliseconds
        socketConnectTimeout = (int) TimeUnit.SECONDS.toMillis(connectTimeout);
        // Blocking timeout initially identical to connect timeout
        socket.setSoTimeout(socketConnectTimeout);
      } else {
        // socket connect timeout is not set, so indefinite (0)
        socketConnectTimeout = 0;
        // Blocking timeout to normal socket timeout, 0 if not set
        socket.setSoTimeout(Math.max(attachProperties.getSoTimeout(), 0));
      }

      final int socketBufferSize = attachProperties.getSocketBufferSize();
      if (socketBufferSize != IConnectionProperties.DEFAULT_SOCKET_BUFFER_SIZE) {
        socket.setReceiveBufferSize(socketBufferSize);
        socket.setSendBufferSize(socketBufferSize);
      }

      socket.connect(new InetSocketAddress(getServerName(), getPortNumber()), socketConnectTimeout);
    } catch (SocketTimeoutException ste) {
      throw new FbExceptionBuilder()
          .timeoutException(ISCConstants.isc_network_error)
          .messageParameter(getServerName())
          .cause(ste)
          .toSQLException();
    } catch (IOException ioex) {
      throw new FbExceptionBuilder()
          .exception(ISCConstants.isc_network_error)
          .messageParameter(getServerName())
          .cause(ioex)
          .toSQLException();
    }
  }
Пример #2
0
 /**
  * Resets the socket timeout to the configured socketTimeout. Does nothing if currently not
  * connected.
  *
  * @throws SQLException If the timeout value cannot be changed
  */
 public final void resetSocketTimeout() throws SQLException {
   if (isConnected()) {
     try {
       final int soTimeout = attachProperties.getSoTimeout();
       final int desiredTimeout = soTimeout != -1 ? soTimeout : 0;
       if (socket.getSoTimeout() != desiredTimeout) {
         socket.setSoTimeout(desiredTimeout);
       }
     } catch (SocketException e) {
       // TODO Add SQLState
       throw new SQLException("Unable to change socket timeout (SO_TIMEOUT)", e);
     }
   }
 }