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 :)

5 Years
Discussion Span
Last Post by JamesCherrill

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 id="EMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
		<property name="aliases">
			<util:map id="aliases">
				<entry key="" value="com.myclass" />

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

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

Sorry for the late post.



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,


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)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.