public void establishConnection()

in wrapper/src/main/java/software/amazon/jdbc/plugin/limitless/LimitlessRouterServiceImpl.java [103:169]


  public void establishConnection(final LimitlessConnectionContext context) throws SQLException {
    context.setLimitlessRouters(getLimitlessRouters(
        this.pluginService.getHostListProvider().getClusterId(), context.getProps()));

    if (Utils.isNullOrEmpty(context.getLimitlessRouters())) {
      LOGGER.finest(Messages.get("LimitlessRouterServiceImpl.limitlessRouterCacheEmpty"));
      final boolean waitForRouterInfo = LimitlessConnectionPlugin.WAIT_FOR_ROUTER_INFO.getBoolean(context.getProps());
      if (waitForRouterInfo) {
        synchronouslyGetLimitlessRoutersWithRetry(context);
      } else {
        LOGGER.finest(Messages.get("LimitlessRouterServiceImpl.usingProvidedConnectUrl"));
        if (context.getConnection() == null || context.getConnection().isClosed()) {
          context.setConnection(context.getConnectFunc().call());
        }
        return;
      }
    }

    if (context.getLimitlessRouters().contains(context.getHostSpec())) {
      LOGGER.finest(Messages.get(
          "LimitlessRouterServiceImpl.connectWithHost",
          new Object[] {context.getHostSpec().getHost()}));
      if (context.getConnection() == null  || context.getConnection().isClosed()) {
        try {
          context.setConnection(context.getConnectFunc().call());
        } catch (final SQLException e) {
          retryConnectWithLeastLoadedRouters(context);
        }
      }
      return;
    }

    RoundRobinHostSelector.setRoundRobinHostWeightPairsProperty(
        context.getProps(),
        context.getLimitlessRouters());
    HostSpec selectedHostSpec;
    try {
      selectedHostSpec = this.pluginService.getHostSpecByStrategy(
          context.getLimitlessRouters(),
          HostRole.WRITER,
          RoundRobinHostSelector.STRATEGY_ROUND_ROBIN);
      LOGGER.fine(Messages.get(
          "LimitlessRouterServiceImpl.selectedHost",
          new Object[] {selectedHostSpec != null ? selectedHostSpec.getHost() : "null"}));
    } catch (SQLException e) {
      retryConnectWithLeastLoadedRouters(context);
      return;
    }

    if (selectedHostSpec == null) {
      retryConnectWithLeastLoadedRouters(context);
      return;
    }

    try {
      context.setConnection(this.pluginService.connect(selectedHostSpec, context.getProps(), context.getPlugin()));
    } catch (SQLException e) {
      if (selectedHostSpec != null) {
        LOGGER.fine(Messages.get(
            "LimitlessRouterServiceImpl.failedToConnectToHost",
            new Object[] {selectedHostSpec.getHost()}));
        selectedHostSpec.setAvailability(HostAvailability.NOT_AVAILABLE);
      }
      // Retry connect prioritising healthiest router for best chance of connection over load-balancing with round-robin
      retryConnectWithLeastLoadedRouters(context);
    }
  }