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

huangapple go评论80阅读模式
英文:

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&lt;Student, UUID&gt; {
    @EntityGraph(&quot;Student.withProgramAndSchedule&quot;)
    @Query(&quot;from Student s&quot;)
    Iterable&lt;Student&gt; 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.

huangapple
  • 本文由 发表于 2020年8月26日 14:44:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63591920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定