使用EclipseLink与Streams一起会导致NullPointerException。

huangapple 未分类评论51阅读模式
英文:

Using Eclipselink with Streams gives NullPointerException

问题

我们有一个使用Eclipselink和JPA的应用程序。假设我们有一个Family对象,它与一个Person对象有一对多的关系。我们从数据库检索一组Person对象,然后使用Java 8的Streams和spliterator并行处理这些Person对象。我们最终遇到了一个空指针异常,经过在Eclipselink代码中的调查,似乎是由两个线程在同时尝试实例化相同的Family克隆对象,但操作不同的Person对象引起的。

从谷歌搜索的结果来看,以这种方式使用Eclipselink的编织和延迟加载似乎不是线程安全的,这是否正确?如果是这样,那在仍然保留一定程度的并行处理的情况下,最佳的处理方式是什么呢?

谢谢,

David

英文:

we have an application that uses Eclipselink weaving with JPA. Let's say we have a Family object that has a one to many relationship with a Person object. We retrieve a set of Person objects from the database and then use Java 8 Streams and a spliterator to process the Person objects in parallel. We end up getting a NullPointerException which from poking around in the Eclipselink code appears to have been caused by two threads operating on different Person objects but trying to instantiate the same Family clone object at the same time.

It appears from googling that Eclipselink using weaving and lazy loading in this manner is not thread safe, is that correct? If so, what's the best way of handling this whilst still retaining some degree of parallelisation?

Thanks,

David

答案1

得分: 0

从描述中并不清楚 - 你是在读取单个Person实例,然后在不同的线程之间共享它吗?

从单个EntityManager读取的对象不是线程安全的 - 它们由该EntityManager进行管理和跟踪,该EntityManager旨在表示单个进程的工作单元。每个线程都需要从自己的EntityManager读取对象;这被配置为从共享缓存中复制实体。否则,如果你需要将对象缓存以在不同的线程之间使用,你可以确保整个图被获取,或者在惰性获取的部分上添加同步。

英文:

It isn't clear from the description - are you reading a single Person instance and then sharing that among different threads?

Objects read from a single EntityManager are not thread safe - they are managed and tracked by that EntityManager, which is meant to represent a single process' unit of work. Every thread needs to read the objects from their own EntityManager; this is configured to make copies of entities from the shared cache. Otherwise, if you need to cache an object for use among different threads, you can make sure the entire graph is fetched or add synchronization on lazily fetched parts yourself.

huangapple
  • 本文由 发表于 2020年6月5日 22:21:39
  • 转载请务必保留本文链接:https://java.coder-hub.com/62217542.html
匿名

发表评论

匿名网友

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

确定