类 AbstractCursorItemReader<T>

  • 所有已实现的接口:
    ItemReader<T>, ItemStream, ItemStreamReader<T>, org.springframework.beans.factory.InitializingBean
    直接已知子类:
    JdbcCursorItemReader, StoredProcedureItemReader

    public abstract class AbstractCursorItemReader<T>
    extends AbstractItemCountingItemStreamItemReader<T>
    implements org.springframework.beans.factory.InitializingBean

    Abstract base class for any simple item reader that opens a database cursor and continually retrieves the next row in the ResultSet.

    By default the cursor will be opened using a separate connection. The ResultSet for the cursor is held open regardless of commits or roll backs in a surrounding transaction. Clients of this reader are responsible for buffering the items in the case that they need to be re-presented on a rollback. This buffering is handled by the step implementations provided and is only a concern for anyone writing their own step implementations.

    There is an option (setUseSharedExtendedConnection(boolean) that will share the connection used for the cursor with the rest of the step processing. If you set this flag to true then you must wrap the DataSource in a ExtendedConnectionDataSourceProxy to prevent the connection from being closed and released after each commit performed as part of the step processing. You must also use a JDBC driver supporting JDBC 3.0 or later since the cursor will be opened with the additional option of 'HOLD_CURSORS_OVER_COMMIT' enabled.

    Each call to AbstractItemCountingItemStreamItemReader.read() will attempt to map the row at the current position in the ResultSet. There is currently no wrapping of the ResultSet to suppress calls to next(). However, if the RowMapper (mistakenly) increments the current row, the next call to read will verify that the current row is at the expected position and throw a DataAccessException if it is not. The reason for such strictness on the ResultSet is due to the need to maintain control for transactions and restartability. This ensures that each call to AbstractItemCountingItemStreamItemReader.read() returns the ResultSet at the correct row, regardless of rollbacks or restarts.

    ExecutionContext: The current row is returned as restart data, and when restored from that same data, the cursor is opened and the current row set to the value within the restart data. See setDriverSupportsAbsolute(boolean) for improving restart performance.

    Calling close on this ItemStream will cause all resources it is currently using to be freed. (Connection, ResultSet, etc). It is then illegal to call AbstractItemCountingItemStreamItemReader.read() again until it has been re-opened.

    Known limitation: when used with Derby setVerifyCursorPosition(boolean) needs to be false because ResultSet.getRow() call used for cursor position verification is not available for 'TYPE_FORWARD_ONLY' result sets.

    作者:
    Lucas Ward, Peter Zozom, Robert Kasanicky, Thomas Risberg, Michael Minella, Mahmoud Ben Hassine
    • 字段概要

      字段 
      修饰符和类型字段说明
      protected org.apache.commons.logging.Loglog
      Logger available to subclasses
      protected java.sql.ResultSetrs 
      static intVALUE_NOT_SET 
    • 方法概要

      所有方法 实例方法 抽象方法 具体方法 
      修饰符和类型方法说明
      voidafterPropertiesSet()
      Assert that mandatory properties are set.
      protected voidapplyStatementSettings​(java.sql.PreparedStatement stmt)
      Prepare the given JDBC Statement (or PreparedStatement or CallableStatement), applying statement settings such as fetch size, max rows, and query timeout.
      protected abstract voidcleanupOnClose() 
      protected voiddoClose()
      Close the cursor and database connection.
      protected voiddoOpen()
      Execute the statement to open the cursor.
      protected TdoRead()
      Read next row and map it to item, verify cursor position if setVerifyCursorPosition(boolean) is true.
      javax.sql.DataSourcegetDataSource()
      Public getter for the data source.
      protected org.springframework.jdbc.support.SQLExceptionTranslatorgetExceptionTranslator()
      Creates a default SQLErrorCodeSQLExceptionTranslator for the specified DataSource if none is set.
      abstract java.lang.StringgetSql() 
      protected voidhandleWarnings​(java.sql.Statement statement)
      Throw a SQLWarningException if we're not ignoring warnings, else log the warnings (at debug level).
      protected voidinitializeConnection() 
      booleanisUseSharedExtendedConnection() 
      protected voidjumpToItem​(int itemIndex)
      Use ResultSet.absolute(int) if possible, otherwise scroll by calling ResultSet.next().
      protected abstract voidopenCursor​(java.sql.Connection con) 
      protected abstract TreadCursor​(java.sql.ResultSet rs, int currentRow)
      Read the cursor and map to the type of object this reader should return.
      voidsetConnectionAutoCommit​(boolean autoCommit)
      Set whether "autoCommit" should be overridden for the connection used by the cursor.
      voidsetDataSource​(javax.sql.DataSource dataSource)
      Public setter for the data source for injection purposes.
      voidsetDriverSupportsAbsolute​(boolean driverSupportsAbsolute)
      Indicate whether the JDBC driver supports setting the absolute row on a ResultSet.
      voidsetFetchSize​(int fetchSize)
      Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for this ResultSet object.
      voidsetIgnoreWarnings​(boolean ignoreWarnings)
      Set whether SQLWarnings should be ignored (only logged) or exception should be thrown.
      voidsetMaxRows​(int maxRows)
      Sets the limit for the maximum number of rows that any ResultSet object can contain to the given number.
      voidsetQueryTimeout​(int queryTimeout)
      Sets the number of seconds the driver will wait for a Statement object to execute to the given number of seconds.
      voidsetUseSharedExtendedConnection​(boolean useSharedExtendedConnection)
      Indicate whether the connection used for the cursor should be used by all other processing thus sharing the same transaction.
      voidsetVerifyCursorPosition​(boolean verifyCursorPosition)
      Allow verification of cursor position after current row is processed by RowMapper or RowCallbackHandler.
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 字段详细资料

      • log

        protected final org.apache.commons.logging.Log log
        Logger available to subclasses
      • rs

        protected java.sql.ResultSet rs
    • 方法详细资料

      • afterPropertiesSet

        public void afterPropertiesSet()
                                throws java.lang.Exception
        Assert that mandatory properties are set.
        指定者:
        afterPropertiesSet 在接口中 org.springframework.beans.factory.InitializingBean
        抛出:
        java.lang.IllegalArgumentException - if either data source or SQL properties not set.
        java.lang.Exception
      • setDataSource

        public void setDataSource​(javax.sql.DataSource dataSource)
        Public setter for the data source for injection purposes.
        参数:
        dataSource - DataSource to be used
      • getDataSource

        public javax.sql.DataSource getDataSource()
        Public getter for the data source.
        返回:
        the dataSource
      • applyStatementSettings

        protected void applyStatementSettings​(java.sql.PreparedStatement stmt)
                                       throws java.sql.SQLException
        Prepare the given JDBC Statement (or PreparedStatement or CallableStatement), applying statement settings such as fetch size, max rows, and query timeout. @param stmt the JDBC Statement to prepare
        参数:
        stmt - PreparedStatement to be configured
        抛出:
        java.sql.SQLException - if interactions with provided stmt fail
        另请参阅:
        setFetchSize(int), setMaxRows(int), setQueryTimeout(int)
      • getExceptionTranslator

        protected org.springframework.jdbc.support.SQLExceptionTranslator getExceptionTranslator()
        Creates a default SQLErrorCodeSQLExceptionTranslator for the specified DataSource if none is set.
        返回:
        the exception translator for this instance.
      • handleWarnings

        protected void handleWarnings​(java.sql.Statement statement)
                               throws org.springframework.jdbc.SQLWarningException,
                                      java.sql.SQLException
        Throw a SQLWarningException if we're not ignoring warnings, else log the warnings (at debug level).
        参数:
        statement - the current statement to obtain the warnings from, if there are any.
        抛出:
        java.sql.SQLException - if interaction with provided statement fails.
        org.springframework.jdbc.SQLWarningException
        另请参阅:
        SQLWarningException
      • setFetchSize

        public void setFetchSize​(int fetchSize)
        Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for this ResultSet object. If the fetch size specified is zero, the JDBC driver ignores the value.
        参数:
        fetchSize - the number of rows to fetch
        另请参阅:
        ResultSet.setFetchSize(int)
      • setMaxRows

        public void setMaxRows​(int maxRows)
        Sets the limit for the maximum number of rows that any ResultSet object can contain to the given number.
        参数:
        maxRows - the new max rows limit; zero means there is no limit
        另请参阅:
        Statement.setMaxRows(int)
      • setQueryTimeout

        public void setQueryTimeout​(int queryTimeout)
        Sets the number of seconds the driver will wait for a Statement object to execute to the given number of seconds. If the limit is exceeded, an SQLException is thrown.
        参数:
        queryTimeout - seconds the new query timeout limit in seconds; zero means there is no limit
        另请参阅:
        Statement.setQueryTimeout(int)
      • setIgnoreWarnings

        public void setIgnoreWarnings​(boolean ignoreWarnings)
        Set whether SQLWarnings should be ignored (only logged) or exception should be thrown.
        参数:
        ignoreWarnings - if TRUE, warnings are ignored
      • setVerifyCursorPosition

        public void setVerifyCursorPosition​(boolean verifyCursorPosition)
        Allow verification of cursor position after current row is processed by RowMapper or RowCallbackHandler. Default value is TRUE.
        参数:
        verifyCursorPosition - if true, cursor position is verified
      • setDriverSupportsAbsolute

        public void setDriverSupportsAbsolute​(boolean driverSupportsAbsolute)
        Indicate whether the JDBC driver supports setting the absolute row on a ResultSet. It is recommended that this is set to true for JDBC drivers that supports ResultSet.absolute() as it may improve performance, especially if a step fails while working with a large data set.
        参数:
        driverSupportsAbsolute - false by default
        另请参阅:
        ResultSet.absolute(int)
      • setUseSharedExtendedConnection

        public void setUseSharedExtendedConnection​(boolean useSharedExtendedConnection)
        Indicate whether the connection used for the cursor should be used by all other processing thus sharing the same transaction. If this is set to false, which is the default, then the cursor will be opened using in its connection and will not participate in any transactions started for the rest of the step processing. If you set this flag to true then you must wrap the DataSource in a ExtendedConnectionDataSourceProxy to prevent the connection from being closed and released after each commit. When you set this option to true then the statement used to open the cursor will be created with both 'READ_ONLY' and 'HOLD_CURSORS_OVER_COMMIT' options. This allows holding the cursor open over transaction start and commits performed in the step processing. To use this feature you need a database that supports this and a JDBC driver supporting JDBC 3.0 or later.
        参数:
        useSharedExtendedConnection - false by default
      • setConnectionAutoCommit

        public void setConnectionAutoCommit​(boolean autoCommit)
        Set whether "autoCommit" should be overridden for the connection used by the cursor. If not set, defaults to Connection / Datasource default configuration.
        参数:
        autoCommit - value used for Connection.setAutoCommit(boolean).
        从以下版本开始:
        4.0
      • getSql

        public abstract java.lang.String getSql()
      • doClose

        protected void doClose()
                        throws java.lang.Exception
        Close the cursor and database connection. Make call to cleanupOnClose so sub classes can cleanup any resources they have allocated.
        指定者:
        doClose 在类中 AbstractItemCountingItemStreamItemReader<T>
        抛出:
        java.lang.Exception - Allows subclasses to throw checked exceptions for interpretation by the framework
      • cleanupOnClose

        protected abstract void cleanupOnClose()
                                        throws java.lang.Exception
        抛出:
        java.lang.Exception
      • doOpen

        protected void doOpen()
                       throws java.lang.Exception
        Execute the statement to open the cursor.
        指定者:
        doOpen 在类中 AbstractItemCountingItemStreamItemReader<T>
        抛出:
        java.lang.Exception - Allows subclasses to throw checked exceptions for interpretation by the framework
      • openCursor

        protected abstract void openCursor​(java.sql.Connection con)
      • readCursor

        @Nullable
        protected abstract T readCursor​(java.sql.ResultSet rs,
                                        int currentRow)
                                 throws java.sql.SQLException
        Read the cursor and map to the type of object this reader should return. This method must be overridden by subclasses.
        参数:
        rs - The current result set
        currentRow - Current position of the result set
        返回:
        the mapped object at the cursor position
        抛出:
        java.sql.SQLException - if interactions with the current result set fail
      • jumpToItem

        protected void jumpToItem​(int itemIndex)
                           throws java.lang.Exception
        Use ResultSet.absolute(int) if possible, otherwise scroll by calling ResultSet.next().
        覆盖:
        jumpToItem 在类中 AbstractItemCountingItemStreamItemReader<T>
        参数:
        itemIndex - index of item (0 based) to jump to.
        抛出:
        java.lang.Exception - Allows subclasses to throw checked exceptions for interpretation by the framework