Spring JDBC support and large dataset

前端 未结 5 692
眼角桃花
眼角桃花 2020-12-14 02:56

When using one of the various JDBC template methods I am confused on how to iterate/scroll over large result sets (which won\'t fit into memory). Even without a direct expos

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 03:10

    The Oracle JDBC driver has proper support for the setFetchSize() method on java.sql.Statement, which allows you to control how many rows the driver will fetch in one go.

    However, RowMapper as used by Spring works by reading each row into memory, getting the RowMapper to translate it into an object, and storing each row's object in one big list. If your result set is huge, then this list will get big, regardless of how JDBC fetches the row data.

    If you need to handle large result sets, then RowMapper isn't scaleable. You might consider using RowCallbackHandler instead, along with the corresponding methods on JdbcTemplate. RowCallbackHandler doesn't dictate how the results are stored, leaving it up to you to store them.

提交回复
热议问题