michaldo
michaldo

Reputation: 4639

Runtime exception is @ApplicationException(rollback=false) but transaction is finally rolled back

My runtime exception

@ApplicationException(rollback=false)
public class UncheckedException extends RuntimeException {}

My EJB code

@Stateless
@Transactional(TxType.REQUIRES_NEW)
public class ContainerManagedTransactionBean {

    @PersistenceContext EntityManager em;

    public void insertAndThrowUnchecked() throws UncheckedException {
        em.persist(new Entry());
        throw new UncheckedException();
    }

}

My another EJB is client

@Singleton
@Startup
@Transactional(TxType.NOT_SUPPORTED)
public class Start {

    @EJB
    ContainerManagedTransactionBean bean;

    @PostConstruct
    public void start() {
        //...       
        try {
            bean.insertAndThrowUnchecked();
        } catch (Exception e) {
            System.out.println("Start unchecked exception catched");
        }
    }

Could someone explain me why insertAndThrowUnchecked is rolled back?

In similar case, when exception is checked,

@ApplicationException(rollback=false)
public class CheckedException extends Exception {}

transaction is committed.

Working example is at this GitHub link.

I will appreciate clear explanation and link to proper part of EJB specification

Upvotes: 1

Views: 1685

Answers (1)

Brett Kail
Brett Kail

Reputation: 33956

Per section 7.1 of the EJB 3.2 specification:

It is illegal to associate JTA transactional interceptors (see [8]) with Enterprise JavaBeans. The EJB Container should fail deployment of such applications.[39]

[39] This restriction may be removed in a future release of this specification.

Since the @Transactional annotation is incorrectly being used to specify a JTA transaction interceptor on an EJB , the @ApplicationException annotation has no effect. Try using the @TransactionAttribute annotation instead.

Upvotes: 2

Related Questions