英文:
Are Spliterators from Iterable results from Spring JPA safe to use in parallelStream
问题
与https://stackoverflow.com/questions/63469182/are-parallelstreams-on-onetomany-collections-safe 类似,但我的问题特定于Spring JPA Repository查询的结果,例如:
public interface Students extends JpaRepository<Student, UUID> {
@EntityGraph("Student.withProgramAndSchedule")
@Query("from Student s")
Iterable<Student> findAllWithProgramAndSchedule();
}
我能安全地在并行情况下使用它吗?例如:
StreamSupport.stream(students.findAllWithProgramAndSchedule().spliterator(), true)
英文:
Similar to https://stackoverflow.com/questions/63469182/are-parallelstreams-on-onetomany-collections-safe but my question is specific to the result from a Spring JPA Repository query e.g.
public interface Students extends JpaRepository<Student, UUID> {
@EntityGraph("Student.withProgramAndSchedule")
@Query("from Student s")
Iterable<Student> findAllWithProgramAndSchedule();
}
Can I safely use it with parallel? e.g.
StreamSupport.stream(students.findAllWithProgramAndSchedule().spliterator(), true)
答案1
得分: 3
JPA实体管理器及其管理的实体不是线程安全的。
可以这样思考,students.findAllWithProgramAndSchedule()
可能会触发延迟加载。在底层,这将使用不是线程安全的JDBC连接,因此 students.findAllWithProgramAndSchedule()
不能是线程安全的。
英文:
JPA entity managers and the entities they manage are not thread safe.
One way to think about it is that students.findAllWithProgramAndSchedule()
might trigger a lazy load. Under the hood this will use a JDBC connection which itself is not thread safe, therefore the students.findAllWithProgramAndSchedule()
can't be thread safe.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论