2

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 ?

Victor
  • 53
  • 3
  • Duplicate of https://stackoverflow.com/questions/10964717/a-jta-entitymanager-cannot-use-gettransaction – Mohit Mutha Apr 30 '18 at 04:28
  • What is confusing you about the error? The JPA spec is very clear that when you define your datasource as JTA then you don't use LOCAL transactions, and instead commit using the JTA transaction –  Apr 30 '18 at 07:43

0 Answers0