Discussions

EJB programming & troubleshooting: Need help in identifying the transaction context.

  1. I am suspecious about my transaction context. The scenario is as explained below.

    let us say i am having a session bean called 'session1'. I am having two methods in session1 with transaction attributes as explained below

    methodA - requires

    methodB - requiresNew


    I am calling methodB from methodA. But i am doubtfuy that whether methodB executes in different transaction or the same transaction as methodA since method call is made through the same instance of session bean.

    can any body help me to get rid of this confusion.

    regards
    Ram Kumar
  2. The container does exactly what you tell it to do ;)

    Thus, in your case, methodB will run in a different transaction context from methodA.

    But, please, do not confuse it with nested transactions (which are not supported).
    E.g., methodB could be used for some customised auditing of sensitive information access (methodB will always record the operation on the entity). In case there's an error later in methodA, the methodB's transaction will not rollback.

    Hope this sheds some light :)

    Best regards,
    Andrew
  3. thanks Andrew it cleared my confusion. Can you help me know how the new transaction is initialized for methodB even if it is called by a method of the same instance.
  4. It depends on how you are invoking methodB.
    If it is like this
    method_A () {
     method_B();
    }

    Then I don't think a new transaction is started. Instead, method_A's transaction is used.

    method_A () {
    //look up session bean
    RemoteReference ref = locator.lookup();
    ref.method_B();
    }
    In this case, method_B runs in new transaction.

    I referred EJB spec but didn't find exact answer.
    I answered it with my experience though I am not sure...:)