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 that will delete the table directly from the job xml file ?
I am using ibatis as ORM
you mean even more simple than a tasklet, e.g. like this pseudocode ?
<!-- xml bean config -->
<bean id="deleteTableTaskletStep" class="...">
<property name="dataSource" ref="dataSource" />
<property name="sql" value="delete from ..." />
</bean>
// java code
public class DeleteTableTasklet implements Tasklet {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
new JdbcTemplate(this.dataSource).executeQuery(this.sql)
return RepeatStatus.FINISHED;
}
}
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With