public T execute()

in wrapper/src/main/java/software/amazon/jdbc/plugin/efm/HostMonitoringConnectionPlugin.java [135:221]


  public <T, E extends Exception> T execute(
      final Class<T> resultClass,
      final Class<E> exceptionClass,
      final Object methodInvokeOn,
      final String methodName,
      final JdbcCallable<T, E> jdbcMethodFunc,
      final Object[] jdbcMethodArgs)
      throws E {

    // update config settings since they may change
    final boolean isEnabled = FAILURE_DETECTION_ENABLED.getBoolean(this.properties);

    if (!isEnabled || !SubscribedMethodHelper.NETWORK_BOUND_METHODS.contains(methodName)) {
      return jdbcMethodFunc.call();
    }

    final int failureDetectionTimeMillis = FAILURE_DETECTION_TIME.getInteger(this.properties);
    final int failureDetectionIntervalMillis =
        FAILURE_DETECTION_INTERVAL.getInteger(this.properties);
    final int failureDetectionCount = FAILURE_DETECTION_COUNT.getInteger(this.properties);

    initMonitorService();

    T result;
    MonitorConnectionContext monitorContext = null;

    try {
      LOGGER.finest(
          () -> Messages.get(
              "HostMonitoringConnectionPlugin.activatedMonitoring",
              new Object[] {methodName}));

      final HostSpec monitoringHostSpec = this.getMonitoringHostSpec();

      monitorContext =
          this.monitorService.startMonitoring(
              this.pluginService.getCurrentConnection(), // abort this connection if needed
              monitoringHostSpec.asAliases(),
              monitoringHostSpec,
              this.properties,
              failureDetectionTimeMillis,
              failureDetectionIntervalMillis,
              failureDetectionCount);

      result = jdbcMethodFunc.call();

    } finally {
      if (monitorContext != null) {
        monitorContext.getLock().lock();
        try {
          this.monitorService.stopMonitoring(monitorContext);

          if (monitorContext.isNodeUnhealthy()) {
            this.pluginService.setAvailability(
                this.getMonitoringHostSpec().asAliases(),
                HostAvailability.NOT_AVAILABLE);

            final boolean isConnectionClosed;
            try {
              isConnectionClosed = this.pluginService.getCurrentConnection().isClosed();
            } catch (final SQLException e) {
              throw castException(exceptionClass, e);
            }

            if (!isConnectionClosed) {
              abortConnection();
              throw castException(
                  exceptionClass,
                  new SQLException(
                      Messages.get(
                          "HostMonitoringConnectionPlugin.unavailableNode",
                          new Object[] {this.pluginService.getCurrentHostSpec().asAlias()})));
            }
          }
        } finally {
          monitorContext.getLock().unlock();
        }

        LOGGER.finest(
            () -> Messages.get(
                "HostMonitoringConnectionPlugin.monitoringDeactivated",
                new Object[] {methodName}));
      }
    }

    return result;
  }