3

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

user2434
  • 6,339
  • 18
  • 63
  • 87

3 Answers3

19

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;
}
}
Michael Pralow
  • 6,560
  • 2
  • 30
  • 46
  • I did this... only if I could have read the question before I would have answered it first Michael :) :) :) :) – Vicky Oct 20 '11 at 13:41
  • Could you add the best way insure that this only runs once at the beginning of the job? – k-den Mar 20 '13 at 19:15
  • a [taskletstep](http://static.springsource.org/spring-batch/2.1.x/reference/html/configureStep.html#taskletStep) is used like any other step, just use it (once) as first step in a [job configuration](http://static.springsource.org/spring-batch/2.1.x/reference/html/configureJob.html#configuringAJob) – Michael Pralow Mar 20 '13 at 23:46
  • I have the same issue, so why is that when i try to send a datasource to tasklet i get an exception? – Shilan Feb 27 '17 at 08:40
4

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";
salerokada
  • 334
  • 3
  • 7
-2

FYI, Instead of a tasklet, you can use the <jdbc:initialize-database> to point to an initialization script with all your SQL queries used to initialize the db. That way, the queries will be easier to maintain.

<!-- xml bean config -->
<jdbc:initialize-database data-source="dataSource">
       <jdbc:script location="file:C:/db/initial-query.sql" />
</jdbc:initialize-database>

Just remember to include this at the top

<beans xmlns="http://www.springframework.org/schema/beans"
       ...
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="...
           http://www.springframework.org/schema/jdbc
           http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
eureka
  • 509
  • 1
  • 5
  • 9