Java EE7 consists of a bunch of "bean" definitions:
- Managed Beans 1.0 (JSR-316 / JSR-250)
- Dependency Injection for Java 1.0 (JSR-330)
- CDI 1.1 (JSR-346)
- JSF Managed Beans 2.2 (JSR-344)
- EJB 3.2 (JSR-345)
In order to get rid of the chaos in my mind, I studies several articles of "when to use which bean type". One of the pros for EJB seems to be that they alone support declarative container-managed transactions (the famous transaction annotations). I'm not sure, though, if this is correct. Can anyone approve this?
Meanwhile, I came up with a simple demo application to check if this was actually true. I just defined a CDI bean (not an EJB - it has no class level annotations) as follows, based on this snippet:
public class CdiBean {
@Resource
TransactionSynchronizationRegistry tsr;
@Transactional(Transactional.TxType.REQUIRED)
public boolean isTransactional() {
return tsr.getTransactionStatus() == Status.STATUS_ACTIVE;
}
}
Now, the outcome on GlassFish 4.0 is that this method actually returns true, which, according to my inquiries, is not working as expected. I did expect the container to ignore the @Transactional annotation on a CDI bean method, or to even throw an exception. I use a freshly-installed GlassFish 4 server, so there are no interferences.
So my question is really:
- Which bean types do actually support container-managed transactions?
- Just for the sake of curiosity, how could I test it with a simple demo application if the code above is wrong?
(BTW: Someone described a similar problem here, but its solution does not apply to my case.