EntityManager is null when use in parallelStream

感情迁移 提交于 2020-03-06 09:43:13

问题


I have an EJB bean which have a method that call another method from the bean with parallel stream. It works fine, but in the async method I must update entity in the db and when I call entityManager.merge than I got a NullPointerExpcetion. I don't know why.

I use OpenJPA on WebSphere.

The original question was: Call a method for List in parallel?

@Stateless
public class TestBean implements LocalInterface, RemoterInterface {
    @PersistenceContext(unitName = Constants.PERSISTENCE_UNIT)
    protected EntityManager entityManager;

    public void a(List<Object> list) {
        for(Object o : list) {
            asynchMethod(o); // this n method call must run in the same time
        }

        // wait for all asynchMethod result
        /**
         * ...other statements
         */
    }

    private void asynchMethod(Object o) {
        o.setSomeField(value);
        entityManager.merge(o); // NullPointerException
    }
}

来源:https://stackoverflow.com/questions/60259421/entitymanager-is-null-when-use-in-parallelstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!