title: Handling multiple StoredProcedureItemReader calls
body: <p>I have to implement below use case in a Spring Batch job:</p>
  
  <ol>
  <li>Read a list of providers via <code>StoredProcedureItemReader</code></li>
  <li>Iterate through the list and call another <code>StoredProcedureItemReader</code> for each provider (as input parameter) found in step 1.</li>
  <li>The output of second SP is to be written to a CSV.</li>
  </ol>
  
  <p>I have come up with below strategy:</p>
  
  <ol>
  <li>Step 1 starts</li>
  <li>SP ItemReader returns a list of providers.</li>
  <li>In the ItemWriter, save the providers to <code>ExecutionContext</code></li>
  <li>Step 1 ends</li>
  <li>Step 2 starts</li>
  <li>Another SP ItemReader accesses the providers from <code>ExecutionContext</code></li>
  <li>Another ItemWriter writes the response to a CSV using FlatFileItemWriter</li>
  </ol>
  
  <p>I am having trouble understanding how the second SP ItemReader will access the list of providers returned by the first SP ItemReader. Will a partition help here? Also, is there a better strategy to accomplish this?</p>
  
  <p>EDIT 1:</p>
  
  <p>Here is the original implementation of ItemProcessor (as enrichment):</p>
  
  <pre><code>@Scope("step")
  public class FetchReportFromProviderProcessor implements
          ItemProcessor&lt;RiscProvider, List&lt;SogReportRecord&gt;&gt;, ItemStream {
  
      StoredProcedureItemReader&lt;SogReportRecord&gt; reader = new StoredProcedureItemReader&lt;SogReportRecord&gt;();
      private DataSource dataSource;
  
      @Value("#{jobParameters['date']}")
      private String date;
  
      @Override
      public List&lt;SogReportRecord&gt; process(final RiscProvider item) throws Exception {
  
          SogReportRecord record = null;
          List&lt;SogReportRecord&gt; records = new ArrayList&lt;SogReportRecord&gt;();
  
          SqlParameter[] sqlParameters = new SqlParameter[] {new SqlParameter(OracleTypes.CURSOR)};           
  
          reader.setParameters(sqlParameters);
          reader.setPreparedStatementSetter(new PreparedStatementSetter() {
  
              @Override
              public void setValues(PreparedStatement ps) throws SQLException {
                  ps.setString(0, item.getPrefix());
                  ps.setString(1, date);
              }
          });
  
          while( (record = reader.read()) != null ) {
              records.add(record);
          }
  
          return records;
      }
  
      public DataSource getDataSource() {
          return dataSource;
      }
  
      public void setDataSource(DataSource dataSource) {
          this.dataSource = dataSource;
      }
  
      @Override
      public void open(ExecutionContext executionContext)
              throws ItemStreamException {
          reader.setDataSource(dataSource);
          reader.setProcedureName("RISC_GET_DAYMOVEINOUT");
          reader.open(executionContext);      
      }
  
      @Override
      public void update(ExecutionContext executionContext)
              throws ItemStreamException {
          reader.update(executionContext);        
      }
  
      @Override
      public void close() throws ItemStreamException {
          reader.close();     
      }
  
  }
  </code></pre>
  
  <p>And the XML part:</p>
  
  <pre><code>&lt;batch:job id="SOG_MOVEINOUT_REPORT_GENERATOR"&gt;
      &lt;batch:step id="GET_REPORTS"&gt;
          &lt;batch:tasklet&gt;
              &lt;batch:chunk reader="getProviders"
                  processor="fetchRecordsFromProvider"
                  writer="sogReportWriter" commit-interval="500" /&gt;
          &lt;/batch:tasklet&gt;
      &lt;/batch:step&gt;
  &lt;/batch:job&gt;
  
  &lt;!-- Reader to fetch list of providers --&gt;
  &lt;bean id="getProviders" class="org.springframework.batch.item.database.StoredProcedureItemReader"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="procedureName" value="RISC_GET_PROVIDER" /&gt;
      &lt;property name="parameters"&gt;
          &lt;list&gt;
              &lt;bean class="org.springframework.jdbc.core.SqlOutParameter"&gt;
                  &lt;constructor-arg index="0" value="providers" /&gt;
                  &lt;constructor-arg index="1"&gt;
                      &lt;util:constant static-field="oracle.jdbc.OracleTypes.CURSOR" /&gt;
                  &lt;/constructor-arg&gt;
              &lt;/bean&gt;
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="refCursorPosition" value="1" /&gt;
      &lt;property name="rowMapper"&gt;
          &lt;bean class="com.kpn.risc.ProviderRowMapper" /&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="fetchRecordsFromProvider" class="com.kpn.risc.FetchReportFromProviderProcessor"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="sogReportWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step"&gt;
      &lt;property name="resource" value="file:///${batch.job.report.dir}/report-#{stepExecutionContext['provider']}.csv" /&gt;
      &lt;property name="lineAggregator"&gt;
          &lt;bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator"&gt;
              &lt;property name="fieldExtractor"&gt;
                  &lt;bean class="org.springframework.batch.item.file.transform.PassThroughFieldExtractor" /&gt;
              &lt;/property&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>In the code above, the processor is delegating its work to an SP ItemReader. But the reader is unable to initialize properly before the <code>read()</code> method gets called. Is it possible or recommended to call an <code>ItemReader</code> inside <code>ItemProcessor</code>?</p>