Skip to content

jdbc array resultsets cause issues with read-write-splitting plugin with spring boot #1367

@treetip

Description

@treetip

Describe the bug

In spring boot 3.4.x java.sql.Array#getResultSet causes a "underlying connection has changed" exception when the surrounding transaction/connection is read only.

Expected Behavior

no exception thrown

What plugins are used? What other connection properties were set?

?wrapperDialect=aurora-pg&wrapperProfileName=F0 with postgres 17.4 aurora cluster of 1rw, 1ro node

Current Behavior

Reading a jdbc array via its java.sql.ResultSet throws exception about the read only connection changing.

Reproduction Steps

@Component
@AllArgsConstructor
public FancyDao {

    private final NamedParameterJdbcTemplate jdbc;

    @Transactional(readOnly = true)
    public List<String> getStuffs() {
        var sql = """
                with stuff(text_col) as (
                  values ('a'),('b'),('c'),('d'),('a')
                )
                select array(select distinct text_col from stuff) as text_values""";
        var result = new ArrayList<String>();
        jdbc.query(sql, rs -> {
            result.addAll(collect(rs.getArray("text_values"), ResultSet::getString, Collectors.toList()));
        });
        return result;
    }

    static <T, A, R> R collect(Array array, RowMapper<T> mapper, Collector<T, A, R> collector) throws SQLException {
        var accumulator = collector.accumulator();
        var container = collector.supplier().get();
        if (array != null) {
            try (var rs = array.getResultSet()) {
                while (rs.next()) {
                    accumulator.accept(container, mapper.mapRow(rs, 2));
                }
            }
        }
        return collector.finisher().apply(container);
    }
}

Possible Solution

Not familiar with the JDBC spec, but the Array data should already be client side at this point, making checking the underlying connection a false positive.

Can also use java.sql.Array#getArray to go around the issue, but it's a bit of a trap.

Additional Information/Context

No response

The AWS Advanced JDBC Driver version used

2.5.6

JDK version used

temurin 21

Operating System and version

fedora 42

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions