diff --git a/src/site/docbook/reference/readersAndWriters.xml b/src/site/docbook/reference/readersAndWriters.xml index 36bda0434..7d8574151 100644 --- a/src/site/docbook/reference/readersAndWriters.xml +++ b/src/site/docbook/reference/readersAndWriters.xml @@ -1716,14 +1716,13 @@ staxItemWriter.write(trade);
Database - Like most enterprise application styles, a database is the - central storage mechanism for batch. However, batch differs from - other application styles due to the sheer size of the datasets - with which the system must work. If a SQL statement returns 1 - million rows, the result set probably holds all returned results - in memory until all rows have been read. Spring Batch provides two - types of solutions for this problem: Cursor and Paging database - ItemReaders. + Like most enterprise application styles, a database is the central + storage mechanism for batch. However, batch differs from other application + styles due to the sheer size of the datasets with which the system must + work. If a SQL statement returns 1 million rows, the result set probably + holds all returned results in memory until all rows have been read. Spring + Batch provides two types of solutions for this problem: Cursor and Paging + database ItemReaders.
Cursor Based ItemReaders @@ -2039,6 +2038,127 @@ itemReader.close(executionContext); <property name="queryString" value="from CustomerCredit" /> </bean>
+ +
+ StoredProcedureItemReader + + Sometimes it is necessary to obtain the cursor data using a + stored procedure. The StoredProcedureItemReader + works like the JdbcCursorItemReader except that + instead of executing a query to obtain a cursor we execute a stored + procedure that returns a cursor. The stored procedure can return the + cursor in three different ways: + + + + as a returned ResultSet (used by SQL Server, Sybase, DB2, + Derby and MySQL) + + + + as a ref-cursor returned as an out parameter (used by Oracle + and PostgreSQL) + + + + as the return value of a stored function call + + + + Below is a basic example configuration using the same 'customer + credit' example as earlier: + + <bean id="reader" class="org.springframework.batch.item.database.StoredProcedureItemReader"> + <property name="dataSource" ref="dataSource"/> + <property name="procedureName" value="sp_customer_credit"/> + <property name="rowMapper"> + <bean class="org.springframework.batch.sample.domain.CustomerCreditRowMapper"/> + </property> +</bean> + + + This example relies on the stored procedure to provide a + ResultSet as a returned result (option 1 above). + + If the stored procedure returned a ref-cursor (option 2) then we + would need to provide the position of the out parameter that is the + returned ref-cursor. Here is an example where the first parameter is + the returned ref-cursor: + + <bean id="reader" class="org.springframework.batch.item.database.StoredProcedureItemReader"> + <property name="dataSource" ref="dataSource"/> + <property name="procedureName" value="sp_customer_credit"/> + <property name="refCursorPosition" value="1"/> + <property name="rowMapper"> + <bean class="org.springframework.batch.sample.domain.CustomerCreditRowMapper"/> + </property> +</bean> + + + If the cursor was returned from a stored function (option 3) we + would need to set the property "function" to + true. It defaults to false. Here + is what that would look like: + + <bean id="reader" class="org.springframework.batch.item.database.StoredProcedureItemReader"> + <property name="dataSource" ref="dataSource"/> + <property name="procedureName" value="sp_customer_credit"/> + <property name="function" value="true"/> + <property name="rowMapper"> + <bean class="org.springframework.batch.sample.domain.CustomerCreditRowMapper"/> + </property> +</bean> + + + In all of these cases we need to define a + RowMapper as well as a + DataSource and the actual procedure + name. + + If the stored procedure or function takes in parameter then they + must be declared and set via the parameters property. Here is an + example for Oracle that declares three parameters. The first one is + the out parameter that returns the ref-cursor, the second and third + are in parameters that takes a value of type INTEGER: + + <bean id="reader" class="org.springframework.batch.item.database.StoredProcedureItemReader"> + <property name="dataSource" ref="dataSource"/> + <property name="procedureName" value="spring.cursor_func"/> + <property name="parameters"> + <list> + <bean class="org.springframework.jdbc.core.SqlOutParameter"> + <constructor-arg index="0" value="newid"/> + <constructor-arg index="1"> + <util:constant static-field="oracle.jdbc.OracleTypes.CURSOR"/> + </constructor-arg> + </bean> + <bean class="org.springframework.jdbc.core.SqlParameter"> + <constructor-arg index="0" value="amount"/> + <constructor-arg index="1"> + <util:constant static-field="java.sql.Types.INTEGER"/> + </constructor-arg> + </bean> + <bean class="org.springframework.jdbc.core.SqlParameter"> + <constructor-arg index="0" value="custid"/> + <constructor-arg index="1"> + <util:constant static-field="java.sql.Types.INTEGER"/> + </constructor-arg> + </bean> + </list> + </property> + <property name="refCursorPosition" value="1"/> + <property name="rowMapper" ref="rowMapper"/> + <property name="preparedStatementSetter" ref="parameterSetter"/> +</bean> + + In addition to the parameter declarations we need to specify a + PreparedStatementSetter implementation that + sets the parameter values for the call. This works the same as for the + JdbcCursorItemReader above. All the additional + properties listed in + apply to the StoredProcedureItemReader as well. + +