Apr 25th, 2012, 07:54 AM
Newbie: Using Itemwriter to update record if insert fails
I am new to Spring Batch. I have a FlatFileItemReader and then write these files to my database using JDBCBatchItemWriter. I will paste the code of the itermwriter. Currently the value of the sql inserts. There are primary keys on my table, so if the insert fails because of data integrity, I would like to update the record instead. I have read up a bit and came across skippable exceptions and composite writers, but can't really see how to get the concepts together. What is the best way to do this? Examples would also help as I am very new to Spring Batch concepts.
<beans:bean id="ItemWriter.CardTransactionDataJob" class="org.springframework.batch.item.database.Jdb cBatchItemWriter">
<beans:property name="assertUpdates" value="false"/>
<beans:bean name="sqlParameterSourceProvider" id="sqlParameterSourceProvider"
<beans:property name="sql" value="insert into CARD_TRN (member_id,member_item_id,card_account_id,display_ name,item_account_id)
values(:memberId, :memberItemId, :cardAccountId, :displayName, :itemAccountId)"/>
<beans:property name="dataSource" ref="DataSourceBean.DEV.dev1"/>
Last edited by Zenoe; Apr 25th, 2012 at 08:13 AM.
Apr 25th, 2012, 11:20 AM
I don't believe that JdbcBatchItemWriter itself can handle the situation you want.
It's possible that you could write a more complex sql statement which would do either/both an insert, and/or an update. The other option is to write your own version of ItemWriter, which could then chose which what sql to run, or could run an update on certain exceptions.