0

I'm trying to use Java's parallel streams to retrieve records form postgres using Spring Data JPA. If I use the non parallel stream method I get the expected number of results returned. If I use the parallel parallelStream method I get one less returned with the same data. I'm unable to show production code but below is an example of what I'm doing.

  List<JpaEntityObject> duplicateEntityList =
  mapOfNewEntities.entrySet().parallelStream()
          .flatMap(key -> getDuplicateRecordsUsingJpa(key.getKey()).stream())
          .collect(Collectors.toList());
whizkey
  • 11
  • 2
  • 1
    If you want us to explain why `getDuplicateRecordsUsingJpa` isn't thread safe you'll need to show us what you are doing in there. One reason might be that the JPA EntityManagers and by extension JPA entities aren't thread safe. – Jens Schauder May 18 '22 at 11:32
  • Currently for testing that method only contains a spring data jpa findBy method call using the passed in key value. My understanding is this should be thread safe. It is also strange the parameter that returns no result is the same one every time. – whizkey May 18 '22 at 14:47

0 Answers0