public EnumSet setCurrentConnection()

in wrapper/src/main/java/software/amazon/jdbc/PluginServiceImpl.java [267:345]


  public EnumSet<NodeChangeOptions> setCurrentConnection(
      final @NonNull Connection connection,
      final @NonNull HostSpec hostSpec,
      @Nullable final ConnectionPlugin skipNotificationForThisPlugin)
      throws SQLException {

    connectionSwitchLock.lock();
    try {

      if (this.currentConnection == null) {
        // setting up an initial connection

        this.currentConnection = connection;
        this.currentHostSpec = hostSpec;
        this.sessionStateService.reset();

        final EnumSet<NodeChangeOptions> changes = EnumSet.of(NodeChangeOptions.INITIAL_CONNECTION);
        this.pluginManager.notifyConnectionChanged(changes, skipNotificationForThisPlugin);

        return changes;

      } else {
        // update an existing connection

        final EnumSet<NodeChangeOptions> changes = compare(this.currentConnection, this.currentHostSpec,
            connection, hostSpec);

        if (!changes.isEmpty()) {

          final Connection oldConnection = this.currentConnection;
          final boolean isInTransaction = this.isInTransaction;
          this.sessionStateService.begin();

          try {
            this.currentConnection = connection;
            this.currentHostSpec = hostSpec;

            this.sessionStateService.applyCurrentSessionState(connection);
            this.setInTransaction(false);

            if (isInTransaction && PropertyDefinition.ROLLBACK_ON_SWITCH.getBoolean(this.props)) {
              try {
                oldConnection.rollback();
              } catch (final SQLException e) {
                // Ignore any exception
              }
            }

            final EnumSet<OldConnectionSuggestedAction> pluginOpinions = this.pluginManager.notifyConnectionChanged(
                changes, skipNotificationForThisPlugin);

            final boolean shouldCloseConnection =
                changes.contains(NodeChangeOptions.CONNECTION_OBJECT_CHANGED)
                    && !oldConnection.isClosed()
                    && !pluginOpinions.contains(OldConnectionSuggestedAction.PRESERVE);

            if (shouldCloseConnection) {
              try {
                this.sessionStateService.applyPristineSessionState(oldConnection);
              } catch (final SQLException e) {
                // Ignore any exception
              }

              try {
                oldConnection.close();
              } catch (final SQLException e) {
                // Ignore any exception
              }
            }
          } finally {
            this.sessionStateService.complete();
          }
        }
        return changes;
      }
    } finally {
      connectionSwitchLock.unlock();
    }
  }