Question

I came across a piece of code where the bean implementation class has @TransactionManagement(TransactionManagementType.BEAN) annotation wherein methods are annotated with CMT @TransactionAttribute. Is is valid? Can an EJB with BMT persistence use CMT transaction annotation? What will be the behavior at runtime?

Though javadoc http://docs.oracle.com/javaee/6/api/javax/ejb/TransactionAttribute.html says that "It can only be specified if container managed transaction demarcation is used.", specifying it doesn't throw any compilation error. Does it mean that jvm simply ignores it at runtime?

@Stateless( mappedName = "Abc")  
@Remote("AbcRemote.class")  
@Local("AbcLocal.class")  
@TransactionManagement(TransactionManagementType.BEAN)  
public class AbcBean implements AbcLocal, AbcRemote{  

    @Resource  
    private UserTransaction utx;  

    @PersistenceUnit  
    private EntityManagerFactory emf;  

    @Override   
    @TransactionAttribute(TransactionAttributeType.REQUIRED)  
    public Abc getAlpbabets(String name) {  
        EntityManager em = null;  
        try {  
            em = emf.createEntityManager();  
        }  
        catch (RuntimeException re) {  

            throw re;  
        }  
        finally {  

        }  
    }  
}  
Was it helpful?

Solution

If you use CMT, then @TransactionAttribute(TransactionAttributeType.REQUIRED) would tell the container to check for an existing transaction and open one if there is none.

But if you use BMT, then it's your responsibility to do such a thing, so there's no one to observe the annotation above. Since it is still syntactically correct and the class is available, there is no need for the JVM to complain about.

Concerning ignoring annotations, there's a hint in the answer to this question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top