spring-bootspring-batchspring-jdbcrowmapper

Resultset in RowMapper For JdbcCursorItemReader


I'm trying to understand how JdbcCursorItemReader works. I noticed that the ItemReader read() method eventually calls readCursor() method from JdbcCursorItemReader where we pass the complete resultset and current position of record to be read to rowMapper. RowMapper interface documentation says that we aren't supposed to call next on resultset and the resultset param is pre-initialized for current row. How and in which class is spring doing this pre-initialization?

JdbcCursorItemReader - passing complete resultset everytime read() is called enter image description here

RowMapper -


Solution

  • The initialization happens in JdbcCursorItemReader#openCursor. The reader is responsible of the ResultSet's lifecycle as well as advancing the pointer to the next row if any.

    The mapper is given the ResultSet with the pointer already set on the current row and should be only concerned with data mapping. It should not change the ResultSet (for instance by calling next() as mentioned in the Javadoc) since this is the responsibility of the reader.