jakarta-eetransactionsejb-3.0stateful-session-bean

Stateful sessions Beans CMT


Good afternoon in my timezone.

I am preparing to the EJB 3.0 certification exam and i reading the "EJB3 In Action" book. In the Pros and Cons of using BMT transaction type section , it says that "if you are using a stateful session bean and need to maintain a transaction across method calls,BMT will be our only option". I can not understand this sentence. Imagine that we have the following psedo class

 @Stateful
    @TransactionManagement(TransactionManagementType.CONTAINER)
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public class TestBean implements Test{
        public void method1(...){...}
        public void method2(...){...}
        public void method2(...){
           method1();
           method2();

    }
}  

Because it defined at class level that all methods will have the REQUIRED transactionAttribute value, when method3 calls the method1 and method2 , those methods will join the transaction created when calling method3, correct ? Can clarify me in this doubt ? Thanks in advance Best regards


Solution

  • "Maintain a transaction across method calls" - is referring to different method calls from client to your bean, not across calls IN your bean.

    By default, in a session bean, each method call creates separate transaction, which must end with the end of the method (unless called already in provided transaction).

    In stateful session bean you can start the transaction in one method, then your client can call a few methods (still participating in the same transaction) and finally you call method to either commit or rollback the transaction (like checkout or cancel). But to be able to manually manage the transaction like this you need BMT.

    I hope its a bit more clear now.