Spring Batch: Problem with chunk and commit-interval
Hello,
I'm using Spring Batch to read from a file and then to write into a data Base. And there is a file structure example (not real):
98165165132161JeanDupont065400015151
01565466516597JackLor 874854984984
cccccc293000000000000000000000000000
the tow first lines are data lines, and the last is for control:
cccccc means control line
2 the number of lines in the file
93 the sum of values after the name (index 24): 06+87
After reading all lines from the file we have to read the control line and do verification. If OK we commit else we stop (BATCH FAIL).
the problem is that if I have a commit-interval 100 and I have 1000 line in the file. The job will read the 100 first line and it will not find the control line. So the job will stop.
If I keep the verification in the end, the job will commit every 100 line, so if the control is not OK we can't roll back.
I use :
ItemReader: org.springframework.batch.item.file.FlatFileItemRe ader
lineMapper: org.springframework.batch.item.file.mapping.Defaul tLineMapper
lineTokenizer: org.springframework.batch.item.file.transform.Fixe dLengthTokenizer
itemWriter: org.springframework.batch.item.database.JdbcBatchI temWriter
and step config:
<batch:step id="step1">
<batch:tasklet transaction-manager="transactionManager" >
<batch:chunk reader="myItemReader" processor="myProcessor"
writer="myItemWriter" commit-interval="100">
</batch:chunk>
</batch:tasklet>
</batch:step>
can I make a tasklet without a chunk and without commit interval ?
Or can I read by chunk but do commit in the end (if OK).
Thank you in advance.
DefaultResultCompletionPolicy for 1 chunk for entire file
Just learning Spring Batch but I had the same problem. Our transactional requirements are to process the entire file or nothing at all, so I needed a commit-interval of #lines. I used the completion policy idea but found that I could use the provided org.springframework.batch.repeat.policy.DefaultRes ultCompletionPolicy in the chunk attribute chunk-completion-policy. Saved me from writing something custom. I haven't tested exhaustively but seems to be doing what I want.
Code:
<batch:job id="myJob"><batch:step id="processAFile"><batch:tasklet>
<batch:chunk reader="myReader" writer="myFileWriter"
chunk-completion-policy="wholeFileCompletionPolicy"/>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="wholeFileCompletionPolicy" class="org.springframework.batch.repeat.policy.DefaultResultCompletionPolicy"/>