I am trying to run a bit of code inside a thread that will make some requests to the database, i am using jpa without spring. The code is as follows:
@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();
}
}
}
in my tests the moment i try to start the transaction i get a error
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)
Can anyone give a direction or even a better a possible solution for this problem ?