I have steps in the batch job that does different things.
But before I begin all these steps, I need to clear a table. Is there any simple way to write a tasklet tha
For batch Java config. Step:
@Bean
private Step dropTable() {
return stepBuilderFactory
.get("dropTable")
.transactionManager(transactionManager)
.tasklet(dropTableTasklet())
.build();
}
Tasklet:
private Tasklet dropTableTasklet() {
return (contribution, chunkContext) -> {
new JdbcTemplate(this.dataSource).execute(DROP_SCRIPT);
return RepeatStatus.FINISHED;
};
}
Script (SQL server):
private static final String DROP_SCRIPT = "IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES "
+ "WHERE TABLE_NAME = 'some_table') "
+ "BEGIN "
+ " DROP TABLE some_table "
+ "END";