Hi,
I am currently working on project wherein I am inserting thousands of records in database using spring batch. So is there any way where I can handle exceptions in my ItemReader and ItemWriter?
I need to apply logic for failure of records when job executes i.e if I am inserting some 100 records at the end of the day by reading xml file and if 50 of them fail due to some reason, I want to skip these failed records and continue processing remaining records.

I will be glad if you help me accordingly.
Thanks in advance :)

Recommended Answers

All 4 Replies

Show your code. Regular try/catch clause should do the job

I hav not referred any user defined java class as such for inserting records. But called my logic inside my context.xml file using spring batch as below:

<bean  id="ItemReader" class="org.springframework.batch.item.xml.StaxEventItemReader" scope="step">
		<property name="fragmentRootElementName" value="rootname" />
		 <property name="resource" value="file:#{jobExecutionContext['input.file.name']}" />
		<property name="unmarshaller" ref="EMarshaller" />
	</bean>
	 

	

	<bean id="EMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
		<property name="aliases">
			<util:map id="aliases">
				<entry key="" value="com.myclass" />
			</util:map>
		</property>
	</bean> 
	

	<bean id="ItemWriter"
		class="org.springframework.batch.item.database.JdbcBatchItemWriter">
		<property name="assertUpdates" value="false" />
		<property name="itemSqlParameterSourceProvider">
			<bean name="sqlParameterSourceProvider" id="sqlParameterSourceProvider"
				class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
		</property>
		<property name="sql"
			value="INSERT STATEMENT" />
		<property name="dataSource" ref="test-dataSource" />
	</bean>


	<batch:job id="InsertRecords" >
	
		<batch:step id="step0" next="step1"  >
			
		</batch:step>
		
		
		<batch:step id="step1">
			<batch:tasklet   >
				<batch:chunk reader="ItemReader" writer="ItemWriter" commit-interval="2"
					 /> 
			</batch:tasklet>
		</batch:step>
	</batch:job>

Sorry for the late post.

Hi,

you can use <batch:skippable-exception-classes> tag for this perpose. It will skip exception mentioned in this tag. Please search for above tag to get more details about this

thanks and regards,
DKP

Hello bakawas, welcome to DaniWeb
Normally we discourage people from posting to threads that have been dead for a year, but in this case you have posted an answer to the question that will be useful to anyone searching that topic, so thank you very much for your contribution. (ps I upvoted your answer)
JC

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.