Radhika
Radhika

Reputation: 91

Temp tables in spring batch

The query in my reader takes a really long time to fetch results due to multiple table joins. I am considering the option of splitting my query joins, using temp tables if possible. is this a feasible solution ? can spring batch support use of temp tables between the reader, processor and writer ?

Upvotes: 3

Views: 1449

Answers (1)

perumalsamy
perumalsamy

Reputation: 69

Yes it is possible. You should use Same DataSource instance for your reader, writer, processor.

Example:

@Component

public class DataSourceDao{

  DataSource dataSource; 


  public DataSource getDataSource() {
        return dataSource;
    }
    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

}

Reader:

 public class MyReader implements ItemReader<POJO_CLASS> {
      @Autowired
      DataSourceDao dataSource; 

      @Override
           JdbcCursorItemReader<POJO_CLASS> reader= new 
            JdbcCursorItemReader<>();

      public <POJO_CLASS> read() throws Exception, UnexpectedInputException, 
                     ParseException, NonTransientResourceException {

           reader.setDataSource(dataSource.getDataSource());
          // Implement your read logic

      }

}

Writer:

public class YourWriter implements ItemWriter<POJO_CLASS> {

  JdbcBatchItemWriter<POJO_CLASS> writer= new JdbcBatchItemWriter<>();

   @Autowired
  DataSourceDao dataSource; 

 void write(List<? extends POJO_CLASS> POJO)
  {
     writer.setDataSource(dataSource.getDataSource());
     <Your logics...> 

  }

Upvotes: 1

Related Questions