Skip to content

chap08 ex-8-9. UniqueLastNameValidator's lastNames Set and Chunk rollback state were out of sync #22

@mklinkj

Description

@mklinkj

Hello.

In examples 8-9 in Chapter 8,
Doesn't the saved state of UniqueLastNameValidator's lastNames Set not be synchronized accordingly even if the chunk is rolled back?

public class UniqueLastNameValidator extends ItemStreamSupport implements Validator<Customer> {
private Set<String> lastNames = new HashSet<>();
@Override
public void validate(Customer value) throws ValidationException {
if(lastNames.contains(value.getLastName())) {
throw new ValidationException("Duplicate last name was found: " + value.getLastName());
}
this.lastNames.add(value.getLastName());
}

When I tested the example, it looked like this:
(To make the changes easier to see, I changed HashSet to LinkedHashset.)


1st Run

  • BATCH_STEP_EXECUTION_CONTEXT.SHORT_CONTEXT

    {"@class":"java.util.HashMap","customerItemReader.read.count":5,"validator.lastNames":["java.util.LinkedHashSet",["Darrow","Donnelly","Benes","Minella","Gates"]],"batch.taskletType":"org.springframework.batch.core.step.item.ChunkOrientedTasklet","batch.stepType":"org.springframework.batch.core.step.tasklet.TaskletStep"}
    
  • The first chunk ends normally, and the second chunk fails (chunk unit 5).

  • Failure cause: Duplicate last name was found: Darrow - line 6 (based on original file)

    • Warren,L,Darrow,4686 Mt. Lee Drive,St. Louis,NY,94935

2nd Run

  • Remove line 6 with duplicate lastName from input file and run again

    • Warren,L,Darrow,4686 Mt. Lee Drive,St. Louis,NY,94935
  • BATCH_STEP_EXECUTION_CONTEXT.SHORT_CONTEXT

    {"@class":"java.util.HashMap","customerItemReader.read.count":10,"validator.lastNames":["java.util.LinkedHashSet",["Darrow","Donnelly","Benes","Minella","Gates","Williams","Smith","James","Neuberger","Hoover","Gilbert"]],"batch.taskletType":"org.springframework.batch.core.step.item.ChunkOrientedTasklet","batch.stepType":"org.springframework.batch.core.step.tasklet.TaskletStep"}
    
  • ❌ Execution of the 3rd chunk fails, but at this time, Gilbert is saved when it should not have been saved.

  • Failure cause: Duplicate last name was found: Darrow - line 13 (based on original file)


3rd Run

  • Remove line 13 with duplicate LastName from input file

    • Regan,M,Darrow,4851 Nec Av.,Gulfport,MS,33193
  • BATCH_STEP_EXECUTION_CONTEXT.SHORT_CONTEXT

    {"@class":"java.util.HashMap","customerItemReader.read.count":10,"validator.lastNames":["java.util.LinkedHashSet",["Darrow","Donnelly","Benes","Minella","Gates","Williams","Smith","James","Neuberger","Hoover","Gilbert"]],"batch.taskletType":"org.springframework.batch.core.step.item.ChunkOrientedTasklet","batch.stepType":"org.springframework.batch.core.step.tasklet.TaskletStep"}
    
  • Cause of failure: Duplicate last name was found: Gilbert - Because Gilbert was incorrectly saved in the 2nd run, it will continue to fail from now on.

Regarding this issue, shouldn't there be some kind of processing for the lastNames Set, or shouldn't the chunk be 1 for it to work properly?

Thanks. Have a nice day. 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions