apache/shardingsphere · error · SQLFeatureNotSupportedException

setFetchSize

Error message

setFetchSize

What it means

setFetchSize(int rows) on the generated-keys ResultSet always throws SQLFeatureNotSupportedException("setFetchSize") (AbstractUnsupportedGeneratedKeysResultSet:182). The ShardingSphere GeneratedKeysResultSet streams from an in-memory Iterator of key values — there is no driver-level fetch window to size, so the hint API is deliberately unsupported.

Source

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

    
    @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
    public final Blob getBlob(final int columnIndex) throws SQLException {
        throw new SQLFeatureNotSupportedException("getBlob");
    }
    
    @Override
    public final Blob getBlob(final String columnLabel) throws SQLException {
        throw new SQLFeatureNotSupportedException("getBlob");
    }
    
    @Override

View on GitHub (pinned to e952770a21)

Solutions

  1. Apply fetch-size hints to the Statement before execution (statement.setFetchSize(n)) rather than to the generated-keys ResultSet.
  2. Skip the call entirely for key result sets — data is already materialized in memory.
  3. In shared helpers, guard with try-catch (SQLFeatureNotSupportedException) and continue.
  4. Separate INSERT/batch-generated-keys handling from SELECT streaming logic so hints target only query result sets.

Example fix

// before
ResultSet keys = stmt.getGeneratedKeys();
keys.setFetchSize(100); // throws

// after
PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setFetchSize(100);
ResultSet keys = ps.getGeneratedKeys();
Defensive patterns

Strategy: try-catch

Validate before calling

PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setFetchSize(batchSize); // hint goes on the statement

Type guard

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

Try / catch

try {
    rs.setFetchSize(100);
} catch (SQLFeatureNotSupportedException ignore) {
    // keys are already in memory; no fetch window applies
}

Prevention

When it happens

Trigger: Calling resultSet.setFetchSize(n) (e.g. rs.setFetchSize(100) in a batch-insert loop) on the ResultSet returned by getGeneratedKeys() — very common in batch-processing templates that apply fetch-size hints to every result set they touch.

Common situations: Batch/ETL frameworks that set fetch size to control memory on every ResultSet; Spring Batch or streaming readers tuned for MySQL (useFetchSizeTrick) applying hints to key result sets; code that worked against a permissive native driver and throws after switching to ShardingSphere-JDBC.

Related errors


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