PB using JdbcBatchItemWriter with CompositeItemWriter in JAVA code- Spring-Batch

杀马特。学长 韩版系。学妹 提交于 2019-12-10 00:12:55

问题


I have extended the batch service code provided by Official Spring-Batch site - Batch Processing Service and modified the ItemWriter to generate the CSV and to write to the database.

I have used CompositeItemWriter to write in both CSV and Database. However the JdbcBatchItemWriter is not working correctly with CompositeItemWriter. The code is shown below.

    @Bean
public ItemWriter<Person> writer(DataSource dataSource) {
CompositeItemWriter<Person> cWriter = new CompositeItemWriter<Person>();   

// For DataBase 
JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
    writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
    writer.setDataSource(dataSource);

// For CSV
FlatFileItemWriter<Person> csvWriter = new FlatFileItemWriter<Person>();
csvWriter.setResource(new FileSystemResource(new File("./csv/new-data.csv")));
csvWriter.setShouldDeleteIfExists(true);
DelimitedLineAggregator<Person> lineAggregator = new DelimitedLineAggregator<Person>();
lineAggregator.setDelimiter(","); 

BeanWrapperFieldExtractor<Person> fieldExtractor = new BeanWrapperFieldExtractor<Person>();
String[] names = {"firstName", "lastName"};
fieldExtractor.setNames(names);
lineAggregator.setFieldExtractor(fieldExtractor);
csvWriter.setLineAggregator(lineAggregator);

List<ItemWriter<? super Person>> mWriter = new ArrayList<ItemWriter<? super Person>>();
mWriter.add(writer); // **Comment this line and the code works fine**
mWriter.add(csvWriter);
cWriter.setDelegates(mWriter);
    return cWriter;
}

Comment this line - mWriter.add(writer); to run the code. This shows that CompositeItemWriter is working good with FlatFileitemWriter, but not with JdbcBatchItemWriter. The error I am getting is -

or.springframework.jdbc.BadSqlGrammerException: PreparedStatementCallback; bad SQL grammar 
[insert into people(first_name, last_name) VALUES (:firstName, :lastName)]

Caused by: Syntax error in SQl statement "insert into people((first_name, last_name) VALUES (:[*]firstName, :lastName)"; expected"), DEFAULT, NOT, EXISTS, INTERSECTS, SELECT, FROM"; SQL Statement:

How can I resolve JdbcBatchItemWriter to work correctly with CompositeItemWriter ?


回答1:


Its the problem with jdbc auto config which was not running in case of multiple itemWriters. Pls add the last line in same sequence :-

JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
writer.setDataSource(dataSource);

writer.afterPropertiesSet();


来源:https://stackoverflow.com/questions/32656581/pb-using-jdbcbatchitemwriter-with-compositeitemwriter-in-java-code-spring-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!