Class StoredProcedureItemReader<T>
java.lang.Object
org.springframework.batch.item.ItemStreamSupport
org.springframework.batch.item.support.AbstractItemStreamItemReader<T>
org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader<T>
org.springframework.batch.item.database.AbstractCursorItemReader<T>
org.springframework.batch.item.database.StoredProcedureItemReader<T>
- All Implemented Interfaces:
ItemReader<T>
,ItemStream
,ItemStreamReader<T>
,org.springframework.beans.factory.InitializingBean
Item reader implementation that executes a stored procedure and then reads the returned
cursor and continually retrieves the next row in the ResultSet
.
The callable statement used to open the cursor is created with the 'READ_ONLY' option as well as with the '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 class is modeled after the similar JdbcCursorItemReader
class.
This reader is not thread-safe.
- Author:
- Thomas Risberg, Mahmoud Ben Hassine
-
Field Summary
Fields inherited from class org.springframework.batch.item.database.AbstractCursorItemReader
log, rs, VALUE_NOT_SET
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Assert that mandatory properties are set.protected void
cleanupOnClose
(Connection connection) Close the cursor and database connection.getSql()
protected void
openCursor
(Connection con) protected T
readCursor
(ResultSet rs, int currentRow) Read the cursor and map to the type of object this reader should return.void
setFunction
(boolean function) Set whether this stored procedure is a function.void
setParameters
(org.springframework.jdbc.core.SqlParameter[] parameters) Add one or more declared parameters.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.void
setProcedureName
(String sprocedureName) Set the SQL statement to be used when creating the cursor.void
setRefCursorPosition
(int refCursorPosition) Set the parameter position of the REF CURSOR.void
setRowMapper
(org.springframework.jdbc.core.RowMapper<T> rowMapper) Set the RowMapper to be used for all calls to read().Methods inherited from class org.springframework.batch.item.database.AbstractCursorItemReader
applyStatementSettings, doClose, doOpen, doRead, getDataSource, getExceptionTranslator, handleWarnings, initializeConnection, isUseSharedExtendedConnection, jumpToItem, setConnectionAutoCommit, setDataSource, setDriverSupportsAbsolute, setFetchSize, setIgnoreWarnings, setMaxRows, setQueryTimeout, setUseSharedExtendedConnection, setVerifyCursorPosition, translateSqlException
Methods inherited from class org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader
close, getCurrentItemCount, isSaveState, open, read, setCurrentItemCount, setMaxItemCount, setSaveState, update
Methods inherited from class org.springframework.batch.item.ItemStreamSupport
getExecutionContextKey, getName, setExecutionContextName, setName
-
Constructor Details
-
StoredProcedureItemReader
public StoredProcedureItemReader()
-
-
Method Details
-
setRowMapper
Set the RowMapper to be used for all calls to read().- Parameters:
rowMapper
- the RowMapper to use to map the results
-
setProcedureName
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:
sprocedureName
- the SQL used to call the 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
- used to populate the SQL
-
setParameters
public void setParameters(org.springframework.jdbc.core.SqlParameter[] parameters) Add one or more declared parameters. Used for configuring this operation when used in a bean factory. Each parameter will specify SQL type and (optionally) the parameter's name.- Parameters:
parameters
- Array containing the declaredSqlParameter
objects
-
setFunction
public void setFunction(boolean function) Set whether this stored procedure is a function.- Parameters:
function
- indicator
-
setRefCursorPosition
public void setRefCursorPosition(int refCursorPosition) Set the parameter position of the REF CURSOR. Only used for Oracle and PostgreSQL that use REF CURSORs. For any other database this should be kept as 0 which is the default.- Parameters:
refCursorPosition
- The parameter position of the REF CURSOR
-
afterPropertiesSet
Assert that mandatory properties are set.- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
- Overrides:
afterPropertiesSet
in classAbstractCursorItemReader<T>
- Throws:
IllegalArgumentException
- if either data source or SQL properties not set.Exception
-
openCursor
- Specified by:
openCursor
in classAbstractCursorItemReader<T>
-
readCursor
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 classAbstractCursorItemReader<T>
- Parameters:
rs
- The current result setcurrentRow
- 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
Close the cursor and database connection.- Specified by:
cleanupOnClose
in classAbstractCursorItemReader<T>
- Parameters:
connection
- to the database- Throws:
Exception
- If unable to clean up resources
-
getSql
- Specified by:
getSql
in classAbstractCursorItemReader<T>
-