transactionsjava-ee-6entitymanagertimer-jobs

ARJUNA016053: Could not commit transaction - JavaEE6 Timer


We have a timer that triggers a job and do some import / export processing. But on commit we open encounter the error in the title. We've tried several solutions already:

1.) Created 2 class:

@Stateless
public class MyBean { 
    @Inject
    @JpaForJobs
    private EntityManager em;

    @Inject
    private MyService1 service1;

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void migrate(...) {
        service1.create(...);
    }
}
@Startup
@Singleton
public class MyService1 implements Job {
    @Resource
    TimerService timerService;

    @Inject
    private MyBean myBean;

    @Timeout
    public void trigger(Timer timer) {
        migrate();
    }

    private void migrate() {
        myBean.migrate();
    }
}

Works sometimes but when the relationship gets complicated it fail. Example Customer has Student has Parents has Cats.

I also tried bean managed transaction but same ending:

@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class MyBean { 
    @Inject
    @JpaForJobs
    private EntityManager em;

    @Inject
    private MyService1 service1;

    public void migrate(...) {
        utx.begin();
        service1.create(...);
        utx.commit();
    }
}
@Startup
@Singleton
public class MyService1 implements Job {
    @Resource
    TimerService timerService;

    @Inject
    private MyBean myBean;

    @Timeout
    public void trigger(Timer timer) {
        migrate();
    }

    private void migrate() {
        myBean.migrate();
    }
}

Any idea or suggestions? Thanks.


Solution

  • Turns out that approach 1 works and it's just I need to replace:

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    

    with

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    

    Also I have some problem with parsing the xml files. Beware of the lazy fields and BeanUtils.cloneBean.