apache/shardingsphere · error · UnknownSQLException

0

0

Error message

Unknown exception.

What it means

UnknownSQLException (a runtime wrapper) thrown by AbstractPreparedStatementAdapter.setCharacterStream(Reader) when Guava's CharStreams.toString(x) fails with IOException while draining the Reader into a String parameter. The JDBC API declares no checked IOException, so the I/O failure is converted to this 'Unknown exception.' runtime type.

Source

Thrown at jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractPreparedStatementAdapter.java:216

        setParameter(parameterIndex, x);
    }
    
    @Override
    public final void setBinaryStream(final int parameterIndex, final InputStream x, final int length) {
        setParameter(parameterIndex, x);
    }
    
    @Override
    public final void setBinaryStream(final int parameterIndex, final InputStream x, final long length) {
        setParameter(parameterIndex, x);
    }
    
    @Override
    public final void setCharacterStream(final int parameterIndex, final Reader x) {
        try {
            setParameter(parameterIndex, CharStreams.toString(x));
        } catch (final IOException ex) {
            throw new UnknownSQLException(ex);
        }
    }
    
    @Override
    public final void setCharacterStream(final int parameterIndex, final Reader x, final int length) {
        try {
            setParameter(parameterIndex, CharStreams.toString(x));
        } catch (final IOException ex) {
            throw new UnknownSQLException(ex);
        }
    }
    
    @Override
    public final void setCharacterStream(final int parameterIndex, final Reader x, final long length) {
        try {
            setParameter(parameterIndex, CharStreams.toString(x));
        } catch (final IOException ex) {
            throw new UnknownSQLException(ex);

View on GitHub (pinned to e952770a21)

Solutions

  1. Check the Reader is open and at the expected position before binding it; do not reuse an already-consumed Reader.
  2. Read the character data into a String yourself (with explicit error handling) and use setString instead, so the IOException is handled at your call site.
  3. Ensure the underlying stream/file remains open until execute completes, not just until setCharacterStream returns.

Example fix

// before
stmt.setCharacterStream(1, reader);
// after
String text;
try {
    text = CharStreams.toString(reader);
} catch (IOException ex) {
    throw new IllegalArgumentException("bad stream param", ex);
}
stmt.setString(1, text);
Defensive patterns

Strategy: validation

Validate before calling

if (reader == null) throw new IllegalArgumentException(); // and confirm source stream is open

Try / catch

catch (UnknownSQLException ex) { unwrap IOException cause; report which parameter Reader failed; }

Prevention

When it happens

Trigger: Calling setCharacterStream(int, Reader) (single-arg variant) on a ShardingSphere PreparedStatement when the supplied Reader throws while being read — closed Reader, broken pipe on a stream-backed Reader, or interrupted I/O.

Common situations: Passing a Reader over a closed or already-consumed stream (e.g. reading request bodies or files twice); FileReader on a file deleted mid-read; NIO_CharStream decode errors; wrapping stream resources closed by try-with-resources before execute.

Related errors


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