在线程java

时间:2018-04-30 04:10:39

标签: java multithreading jpa

我正在尝试在线程中运行一些代码,这些代码会向数据库发出一些请求,我正在使用没有spring的jpa。代码如下:

@Stateless
public class ResultServiceImpl implements ResultService {

    @Inject
    private ReportService reportService;

    @Inject
    private ReportRepository reportRepository;

    public void processAllReports(){
        Runnable runnable = () -> {
                    ReportDAO dao = (ReportDAO) reportRepository;
                    EntityManager em = dao.getEm().getEntityManagerFactory().createEntityManager();

                    em.getTransaction().begin();
                    reportService.process();
                    em.getTransaction().commit();
                };
                Thread thread = new Thread(runnable);
                thread.start();
            }
    }
}

在我的测试中,当我尝试启动事务时,我收到错误

00:47:49,264 ERROR [stderr] (Thread-121) Exception in thread "Thread-121" java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()

00:47:49,264 ERROR [stderr] (Thread-121)    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.getTransaction(AbstractEntityManagerImpl.java:1333)

00:47:49,264 ERROR [stderr] (Thread-121)    at br.com.govbr.controleinterno.domain.service.impl.UploadServiceImpl.lambda$0(UploadServiceImpl.java:198)

00:47:49,265 ERROR [stderr] (Thread-121)    at java.lang.Thread.run(Unknown Source)

任何人都可以为这个问题提供方向甚至更好的解决方案吗?

0 个答案:

没有答案