private void performCancel()

in src/main/java/software/amazon/documentdb/jdbc/DocumentDbQueryExecutor.java [215:282]


    private void performCancel() throws SQLException {
        final MongoClientSettings settings = connectionProperties.buildMongoClientSettings();
        try (MongoClient client = MongoClients.create(settings)) {
            final MongoDatabase database = client.getDatabase("admin");

            // Find the opId to kill using the queryId.
            final Document currentOp =
                    database.runCommand(
                            new Document("currentOp", 1)
                                    .append("$ownOps", true)
                                    .append("command.comment", queryId));

            if (!(currentOp.get("inprog") instanceof List)) {
                throw new SQLException("Unexpected operation state.");
            }
            final List<?> ops = (List<?>) currentOp.get("inprog");

            // If there are no results, the aggregation has not been executed yet or is complete.
            if (ops.isEmpty()) {
                throw SqlError.createSQLException(
                        LOGGER,
                        SqlState.OPERATION_CANCELED,
                        SqlError.QUERY_NOT_STARTED_OR_COMPLETE);
            }

            // If there is more than 1 result then more than 1 operations have been given same id,
            // and we do not know which to cancel.
            if (ops.size() != 1) {
                throw SqlError.createSQLException(
                        LOGGER,
                        SqlState.OPERATION_CANCELED,
                        SqlError.QUERY_CANNOT_BE_CANCELED,
                        "More than one running operation matched the query ID.");
            }

            if (!(ops.get(0) instanceof Document)) {
                throw new SQLException("Unexpected operation state.");
            }
            final Object opId = ((Document)ops.get(0)).get("opid");

            if (opId == null) {
                throw new SQLException("Unexpected operation state.");
            }

            // Cancel the aggregation using killOp.
            final Document killOp =
                    database.runCommand(new Document("killOp", 1)
                            .append("op", opId));

            // Throw error with info if command did not succeed.
            if (!killOp.get("ok").equals(1.0)) {
                throw SqlError.createSQLException(
                        LOGGER,
                        SqlState.OPERATION_CANCELED,
                        SqlError.QUERY_CANNOT_BE_CANCELED,
                        killOp.get("info"));
            }

        } catch (SQLException e) {
            throw e;
        } catch (Exception e) {
            throw SqlError.createSQLException(
                    LOGGER,
                    SqlState.OPERATION_CANCELED,
                    SqlError.QUERY_CANNOT_BE_CANCELED,
                    e);
        }
    }