Class JdbcCursorItemReader<T>

All Implemented Interfaces:
ItemReader<T>, ItemStream, ItemStreamReader<T>, org.springframework.beans.factory.InitializingBean

public class JdbcCursorItemReader<T> extends AbstractCursorItemReader<T>

Simple item reader implementation that opens a JDBC cursor and continually retrieves the next row in the ResultSet.

The statement used to open the cursor is created with the 'READ_ONLY' option since a non read-only cursor may unnecessarily lock tables or rows. It is also opened with 'TYPE_FORWARD_ONLY' option. By default the cursor will be opened using a separate connection which means that it will not participate in any transactions created as part of the step processing.

Each call to AbstractItemCountingItemStreamItemReader.read() will call the provided RowMapper, passing in the ResultSet.

This reader is not thread-safe.

Author:
Lucas Ward, Peter Zozom, Robert Kasanicky, Thomas Risberg, Mahmoud Ben Hassine
  • Constructor Details

    • JdbcCursorItemReader

      public JdbcCursorItemReader()
  • Method Details

    • setRowMapper

      public void setRowMapper(org.springframework.jdbc.core.RowMapper<T> rowMapper)
      Set the RowMapper to be used for all calls to read().
      Parameters:
      rowMapper - the mapper used to map each item
    • setSql

      public void setSql(String sql)
      Set the SQL statement to be used when creating the cursor. This statement should be a complete and valid SQL statement, as it will be run directly without any modification.
      Parameters:
      sql - SQL statement
    • setPreparedStatementSetter

      public void setPreparedStatementSetter(org.springframework.jdbc.core.PreparedStatementSetter preparedStatementSetter)
      Set the PreparedStatementSetter to use if any parameter values that need to be set in the supplied query.
      Parameters:
      preparedStatementSetter - PreparedStatementSetter responsible for filling out the statement
    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Assert that mandatory properties are set.
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      Overrides:
      afterPropertiesSet in class AbstractCursorItemReader<T>
      Throws:
      IllegalArgumentException - if either data source or SQL properties not set.
      Exception
    • openCursor

      protected void openCursor(Connection con)
      Specified by:
      openCursor in class AbstractCursorItemReader<T>
    • readCursor

      @Nullable protected T readCursor(ResultSet rs, int currentRow) throws SQLException
      Description copied from class: AbstractCursorItemReader
      Read the cursor and map to the type of object this reader should return. This method must be overridden by subclasses.
      Specified by:
      readCursor in class AbstractCursorItemReader<T>
      Parameters:
      rs - The current result set
      currentRow - Current position of the result set
      Returns:
      the mapped object at the cursor position
      Throws:
      SQLException - if interactions with the current result set fail
    • cleanupOnClose

      protected void cleanupOnClose(Connection connection)
      Close the cursor and database connection.
      Specified by:
      cleanupOnClose in class AbstractCursorItemReader<T>
      Parameters:
      connection - to the database
    • getSql

      public String getSql()
      Specified by:
      getSql in class AbstractCursorItemReader<T>