1

I am trying to run my batch job in parallel by utilizing java 8 parallel streams:

 ForkJoinPool forkJoinPool = new ForkJoinPool(8);
 forkJoinPool.execute(() -> tenants.parallelStream()
                .forEach((tenant) -> getDataPerTenant(tenant)));

In getDataPerTenant(tenant), I am using jpa to access database and I am running a bunch of queries. The first problem I encountered was that the ContextClassLoader was null. I then tried to set the ContextClassLoader but followed this post: Parallel stream doesn't set Thread.contextClassLoader after tomcat upgrade and tried creating a ForkJoinPool.

However, threads maintained by ForkJoinPool throw an exception when creating an EntityManager:

No Persistence provider for EntityManager named TestService
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)

I believe this is because no Persistence Providers could be resolved.

Is there anyway to set the persistence provider for a thread? Just to clarify I have a persistence.xml and JPA works when threads are created by web application.

This problem only occurs when I try to spawn new threads that want to access JPA layer.

Any help will be greatly appreciated Thanks!

RyanCW
  • 1,012
  • 2
  • 10
  • 23

0 Answers0