public Connection connect()

in wrapper/src/main/java/software/amazon/jdbc/plugin/failover2/FailoverConnectionPlugin.java [700:760]


  public Connection connect(
      final String driverProtocol,
      final HostSpec hostSpec,
      final Properties props,
      final boolean isInitialConnection,
      final JdbcCallable<Connection, SQLException> connectFunc)
      throws SQLException {

    this.initFailoverMode();

    Connection conn = null;

    if (!ENABLE_CONNECT_FAILOVER.getBoolean(props)) {
      return this.staleDnsHelper.getVerifiedConnection(isInitialConnection, this.hostListProviderService,
            driverProtocol, hostSpec, props, connectFunc);
    }

    final HostSpec hostSpecWithAvailability = this.pluginService.getHosts().stream()
        .filter(x -> x.getHostAndPort().equals(hostSpec.getHostAndPort()))
        .findFirst()
        .orElse(null);

    if (hostSpecWithAvailability == null
        || hostSpecWithAvailability.getAvailability() != HostAvailability.NOT_AVAILABLE) {

      try {
        conn = this.staleDnsHelper.getVerifiedConnection(isInitialConnection, this.hostListProviderService,
            driverProtocol, hostSpec, props, connectFunc);
      } catch (final SQLException e) {
        if (!this.shouldExceptionTriggerConnectionSwitch(e)) {
          throw e;
        }

        this.pluginService.setAvailability(hostSpec.asAliases(), HostAvailability.NOT_AVAILABLE);

        try {
          this.failover();
        } catch (FailoverSuccessSQLException failoverSuccessException) {
          conn = this.pluginService.getCurrentConnection();
        }
      }
    } else {
      try {
        this.pluginService.refreshHostList();
        this.failover();
      } catch (FailoverSuccessSQLException failoverSuccessException) {
        conn = this.pluginService.getCurrentConnection();
      }
    }

    if (conn == null) {
      // This should be unreachable, the above logic will either get a connection successfully or throw an exception.
      throw new SQLException(Messages.get("Failover.unableToConnect"));
    }

    if (isInitialConnection) {
      this.pluginService.refreshHostList(conn);
    }

    return conn;
  }