apache/shardingsphere · error · SQLFeatureNotSupportedException

setFetchDirection

Error message

setFetchDirection

What it means

setFetchDirection(int) is one of the fetch-hint methods AbstractUnsupportedGeneratedKeysResultSet rejects with SQLFeatureNotSupportedException("setFetchDirection") (line 172). ShardingSphere's generated-keys ResultSet iterates an in-memory iterator of key values already computed at execute time, so JDBC fetch-direction hints have no meaning and are explicitly unsupported.

Source

Thrown at jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/unsupported/AbstractUnsupportedGeneratedKeysResultSet.java:172

    
    @Override
    public final void clearWarnings() throws SQLException {
        throw new SQLFeatureNotSupportedException("clearWarnings");
    }
    
    @Override
    public final Reader getCharacterStream(final int columnIndex) throws SQLException {
        throw new SQLFeatureNotSupportedException("getCharacterStream");
    }
    
    @Override
    public final Reader getCharacterStream(final String columnLabel) throws SQLException {
        throw new SQLFeatureNotSupportedException("getCharacterStream");
    }
    
    @Override
    public final void setFetchDirection(final int direction) throws SQLException {
        throw new SQLFeatureNotSupportedException("setFetchDirection");
    }
    
    @Override
    public final int getFetchDirection() throws SQLException {
        throw new SQLFeatureNotSupportedException("getFetchDirection");
    }
    
    @Override
    public final void setFetchSize(final int rows) throws SQLException {
        throw new SQLFeatureNotSupportedException("setFetchSize");
    }
    
    @Override
    public final int getFetchSize() throws SQLException {
        throw new SQLFeatureNotSupportedException("getFetchSize");
    }
    
    @Override

View on GitHub (pinned to e952770a21)

Solutions

  1. Skip fetch-hint calls on generated-keys result sets; iteration order is fixed by the in-memory iterator.
  2. Move the hint to the Statement (statement.setFetchDirection) if the underlying path needs it — or simply drop it for INSERT paths.
  3. Catch SQLFeatureNotSupportedException around framework code you cannot edit and ignore it for key result sets.
  4. Audit generic JDBC helper classes for unconditional setFetchDirection on every ResultSet.

Example fix

// before
rs.setFetchDirection(ResultSet.FETCH_FORWARD);

// after
// no call needed on generated-keys result set; iterator order is fixed
long id = rs.next() ? rs.getLong(1) : -1;
Defensive patterns

Strategy: try-catch

Validate before calling

// apply fetch hints on the Statement before execution, never on the keys ResultSet
PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

Type guard

private static boolean supportsFetchHints(ResultSet rs) {
    return !rs.getClass().getName().endsWith("GeneratedKeysResultSet");
}

Try / catch

try {
    rs.setFetchDirection(ResultSet.FETCH_FORWARD);
} catch (SQLFeatureNotSupportedException ignore) {
    // direction is fixed for in-memory key iteration
}

Prevention

When it happens

Trigger: Calling resultSet.setFetchDirection(ResultSet.FETCH_FORWARD) (or REVERSE/UNKNOWN) on the ResultSet returned by getGeneratedKeys() — often done eagerly by frameworks that set hints on every result set they open (some pooling layers, Spring's JdbcTemplate variants, iBATIS).

Common situations: Frameworks applying uniform fetch hints (e.g. setFetchDirection(FETCH_FORWARD) as an optimization); code tuned for MySQL/PostgreSQL drivers where the call is accepted; the exception appearing only for INSERT+RETURN_GENERATED_KEYS statements after adding ShardingSphere-JDBC.

Related errors


AI-assisted analysis of apache/shardingsphere@e952770a21 (2026-08-14). Data as JSON: /api/errors/9032ef01b97e1118. Report an issue: GitHub.