-1

I searched for some answer to my question on this site; but failed on every turn. I can delete fine if I don't put this in a ExecutorService, but if I do, it doesn't delete. No error occurs just the records are still in the database. Please advise.

 public void deleteAllTrials(List<Trials>list) { 
      threadList = list;
      ExecutorService executor = Executors.newFixedThreadPool(1);
      executor.execute(new Job1());
      executor.shutdown();

}

 public class Job1 implements Runnable {
        @Override
        public void run() {
            //Session session = (Session) entityManager.getDelegate();
            EntityManagerFactory emf = entityManager.getEntityManagerFactory();
            EntityManager em = emf.createEntityManager();
            System.out.println("Size of threadList" + threadList.size());
            long start = System.currentTimeMillis(); 

            for(int i =0; i<threadList.size(); i++){
                System.out.println("In thread...");

                       Trials mergedEntity = em.merge(threadList.get(i));
                em.remove(mergedEntity);
            }
            //System.out.println("Result list in service:" + list.size());
            //em.close();
            long end = System.currentTimeMillis();
            System.out.println("Threads took this long:" + (end - start));
        }
    }
Rika
  • 708
  • 1
  • 5
  • 14
  • Normal you need a transaction for it. em.getTransaction().begin(); ...merge, remove etc. ...em.getTransaction().commit(); – pL4Gu33 May 14 '14 at 19:23
  • @pL4Gu33 Thanks for the reply. I took you advise and I received this error, "A JTA EntityManager cannot use getTransaction()" The "entityManager" variable was initialized by the PersistenceContext annotation with a jta-data-source tag or something. In the app we normally don't need to use getTransactions() and such and commit is done automatically – Rika May 14 '14 at 20:23
  • Ah okay... couldnt see this from the code above. I didnt work with several threads + entitymanager. You could read here hope it helps you: http://stackoverflow.com/questions/14888040/java-an-entitymanager-object-in-a-multithread-environment – pL4Gu33 May 14 '14 at 20:31

2 Answers2

1

I found out that EJBs are more powerful than I thought. If you just add @Asynchronus on top of the method you want the application to separate in a backing thread, it will be acting as a separate thread allowing the user to continue doing what he wants to do without waiting on the process to finish.

 @Asynchronous
 public void deleteAllTrials(List<TrialBillet>list) { 
     List<TrialBillet> threadList = new ArrayList<TrialBillet>();
      threadList = list;
      for(int i =0; i<threadList.size(); i++){
         this.delete(threadList.get(i));
      }


 }
Rika
  • 708
  • 1
  • 5
  • 14
0

If you want to go with Executors, java-ee-7 has introduced ManagedExecutorService

From Java EE 7 tutorial

ManagedExecutorService: A managed executor service is used by applications to execute submitted tasks asynchronously. Tasks are executed on threads that are started and managed by the container. The context of the container is propagated to the thread executing the task. For example, by using an ManagedExecutorService.submit() call, a task, such as the GenerateReportTask, could be submitted to execute at a later time and then, by using the Future object callback, retrieve the result when it becomes available.

code sample:

public class MyClass {

    @Resource
    private ManagedExecutorService mes;


    public void myMethod() {
        mes.execute(new Worker());
    }
}
Aksel Willgert
  • 10,807
  • 5
  • 48
  • 70