简体   繁体   English

来自 Spring JPA 的 Iterable 结果的 Spliterators 是否可以安全地在 parallelStream 中使用

[英]Are Spliterators from Iterable results from Spring JPA safe to use in parallelStream

Similar to Are parallelStreams on OneToMany collections safe?类似于OneToMany 集合上的 parallelStreams 是否安全? but my question is specific to the result from a Spring JPA Repository query eg但我的问题特定于 Spring JPA Repository 查询的结果,例如

public interface Students extends JpaRepository<Student, UUID> {
    @EntityGraph("Student.withProgramAndSchedule")
    @Query("from Student s")
    Iterable<Student> findAllWithProgramAndSchedule();
}

Can I safely use it with parallel?我可以安全地使用并行吗? eg例如

StreamSupport.stream(students.findAllWithProgramAndSchedule().spliterator(), true)

JPA entity managers and the entities they manage are not thread safe. JPA 实体管理器及其管理的实体不是线程安全的。

One way to think about it is that students.findAllWithProgramAndSchedule() might trigger a lazy load.一种思考方式是students.findAllWithProgramAndSchedule()可能会触发延迟加载。 Under the hood this will use a JDBC connection which itself is not thread safe, therefore the students.findAllWithProgramAndSchedule() can't be thread safe.在幕后,这将使用本身不是线程安全的 JDBC 连接,因此students.findAllWithProgramAndSchedule()不能是线程安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM