Skip to content

Support to write via java.io.Reader to use in java.sql.PreparedStatement? #5176

Open
@rdehuyss

Description

@rdehuyss

Is your feature request related to a problem? Please describe.

I think I'm missing something trivial but I would like to stream objects to a PreparedStatement using PreparedStatement.setCharacterStream(int parameterIndex, java.io.Reader reader).

The thing is that SQL drivers are pull based (they start consuming the reader when executeUpdate is called) and Jackson is push based (it writes to a Writer or an OutputStream). Hence, there is no easy way to stream a Java object in JSON format to the database.

All examples out there first map each object to a String causing excess GC and memory allocation that could be avoided.

We could used PipedInputStream and PipedOutputStream and link those but it requires extra thread management which is a road I'm not ready to go down yet.

Describe the solution you'd like

A way that Jackson provides a Reader for a Java object. This Reader would allow to read the Java object as Json.

Usage example

preparedStatement.setCharacterStream(1, objectMapper.writeValueViaReader(object));

Additional context

This is for https://github.com/jobrunr/jobrunr

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions