Spring Batch: Problem with chunk and commit-interval
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):
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:tasklet transaction-manager="transactionManager" >
<batch:chunk reader="myItemReader" processor="myProcessor"
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.
<batch:job id="myJob"><batch:step id="processAFile">
<batch:chunk reader="myReader" writer="myFileWriter"
<bean id="wholeFileCompletionPolicy" class="org.springframework.batch.repeat.policy.DefaultResultCompletionPolicy"/>