I have a Spring batch job to get few rows of data from database to do something.
I wish to do something in first row in RowMapper
.
public myVO mapRow(ResultSet rs, int rowNum) throws SQLException {
System.out.println("rowNum is " + rowNum);
if (rowNum == 1) {
// do something
}
// some other code here
}
This is working fine for me at first. However, after I add in scope="step"
in my batch xml file at reader part, the rowNum
will always 0 instead start of 0 and increase.
Any way to fix this while I wan to maintain the scope="step"
.
The meaning of the Scope = "step" is that it create a instance for each step of execution which means that you will have a new instance for each thread.
So what should be happening is, once you define "step" scope, the map row is called by a new instance of your processor and it has the starting value 0. It is further indicated when you say that it works properly when you remove the step scope.
So what you can do is, keep a static atomic integer (Thread safe) and rely on that counter for your logic rather than relying on the rowNumber.
If this does not make sense, share your configuration for further analysis as well. Thanks.