We are using Spring Batch to read records from CSV file and insert into database table.
Datasource and transaction manager
<!-- connect to database -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="**********" />
<property name="username" value="**********" />
<property name="password" value="**********" />
</bean>
<bean id="transactionManagerTest"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
JOB Configuration
<!-- stored job-meta in database -->
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManagerTest" />
<property name="databaseType" value="Oracle" />
</bean>
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
<property name="taskExecutor">
<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
</property>
</bean>
Below is spring batch job configuration
<batch:job id="reportJob">
<batch:step id="step1" >
<batch:tasklet transaction-manager="transactionManagerTest" >
<batch:chunk reader="cvsFileItemReader" writer="mysqlItemWriter" commit-interval="5" skip-limit="1000" processor-transactional="true">
<!--
<batch:skip-policy>
<bean class="org.springframework.batch.core.step.skip.AlwaysSkipItemSkipPolicy" scope="step"/>
</batch:skip-policy> -->
<batch:skippable-exception-classes>
<batch:include class="java.lang.Exception" />
</batch:skippable-exception-classes>
<!-- <batch:retry-policy>
<bean class="org.springframework.retry.policy.NeverRetryPolicy" scope="step"/>
</batch:retry-policy> -->
<batch:listeners>
<batch:listener ref="itemWriterListner"/>
</batch:listeners>
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
Here we have defined batch:skippable-exception-classes, which should be used to handle if any of record insert statement fails.
Take an example, we have 10 records in csv file, we are reading that one by one and inserting into database table in chuck on 5, but in between, say 4 th record insert fails, it should continue with 5 th records onwards, and should skip only 4 th record.
But with batch:skippable-exception-classes, if 4 th record fails, it again continues from 1 st record. so in database table we have 1-3 records 2 times (duplicate records)
Please suggest, If I am missing any configuration property of spring batch.