Using @Transactional with EJBs

2020-07-20 08:05发布

问题:

Is it ok to use @Transactional with EJBs? Is there any difference between using @TransactionAttribute?

I found already here Difference between @Transactional and @TransactionAttribute that @Transactional is for POJO. I've used it accidently for EJB in Wildfly and there were no errors. I wonder whether the behaviour would be the same or if there are some differences. Or maybe @Transactional would be just ignored and default level of @TransactionAttribute would be used? I've search in the spec but found nothing helpful.

EDIT:

As @kostja suggested it's not clear to what framework question is referred to. I meant @Transactional from CDI, not Spring.

回答1:

No. 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.

From a container implementation perspective, CDI and EJB operate at different layers (the CDI proxy wraps the EJB proxy). In order to make this work nicely, there would need to be deeper integration between the two containers so that the @Transactional interceptor and EJB container don't both attempt to check/update the transaction context.