Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Batch, Update statement in a writer? Throwing IllegalArgumentException

To be as concise as possible,

I have a master step that splits the execution off into parallel slaves.

These slaves each have their own context, which stores the ID of the thread they're executed on, runNumber. They are reader, processor, writer steps. The write of this slave is what I'm concerned with. I have a CompositeItemWriter that has a writer that I want to do an SQL update statement that updates the thread's success in a db. This writer is below:

@Bean
@StepScope
public JdbcBatchItemWriter<TraceDataDto> controlSuccessWriter(
        @Value("#{jobExecutionContext['runNumber']}") int runNumber,
        @Qualifier("veDataSource") DataSource veDataSource) {
    JdbcBatchItemWriter<TraceDataDto> mysqlWriter = new JdbcBatchItemWriter<>();

    mysqlWriter.setDataSource(veDataSource);

    String sql = "UPDATE control SET status=1 WHERE run_number=" + runNumber;

    mysqlWriter.setSql(sql);

    return mysqlWriter;
}

Now, I'm having trouble with this as it spits an error:

java.lang.IllegalArgumentException: Using SQL statement with '?' placeholders requires an ItemPreparedStatementSetter

Now I know there's a path I can go down to create a custom class that implements ItemPreparedStatementSetter but I'm concerned that's going to create unnecessary overhead by mapping single values to objects.

Is there an easy way I can just execute this update statement as part of the slave step?

like image 845
rnmalone Avatar asked Sep 13 '26 20:09

rnmalone


1 Answers

The ItemWriter implementations provided by Spring Batch are expected to persist items. That's not what you're trying to do so I'd recommend creating an ItemWriter (or even better a listener) that just uses JdbcTemplate to execute the query you need to.

like image 68
Michael Minella Avatar answered Sep 16 '26 10:09

Michael Minella