【发布时间】:2014-07-03 00:29:36
【问题描述】:
我在这个网站上搜索了我的问题的一些答案;但每一次都失败了。如果我不把它放在 ExecutorService 中,我可以删除它,但如果我这样做,它不会删除。没有错误发生,只是记录仍在数据库中。请指教。
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));
}
}
【问题讨论】:
-
正常情况下你需要一笔交易。 em.getTransaction().begin(); ...合并、删除等 ...em.getTransaction().commit();
-
@pL4Gu33 感谢您的回复。我接受了你的建议,我收到了这个错误,“A JTA EntityManager cannot use getTransaction()” “entityManager”变量由 PersistenceContext 注释初始化,带有 jta-data-source 标记或其他东西。在应用程序中,我们通常不需要使用 getTransactions() 并且这样的提交是自动完成的
-
啊,好吧...从上面的代码中看不到这一点。我没有使用多个线程+实体管理器。您可以在这里阅读,希望对您有所帮助:stackoverflow.com/questions/14888040/…
标签: jsf-2 executorservice hibernate-entitymanager