views:

604

answers:

2

In the following setup, does method B run in a (new) transaction?

An EJB, having two methods, method A and method B

public class MyEJB implements SessionBean
    public void methodA() {
       doImportantStuff();
       methodB();
       doMoreImportantStuff();
    }

    public void methodB() {
       doDatabaseThing();
    }
}

The EJB is container managed, with methodB in requires_new transaction, and method A in required transaction. thus:

<container-transaction id="MethodTransaction_1178709616940">
  <method id="MethodElement_1178709616955">
    <ejb-name>MyName</ejb-name>
    <method-name>*</method-name>
  <trans-attribute>Required</trans-attribute>
  </method>
  <method id="MethodElement_1178709616971">
    <ejb-name>MyName</ejb-name>
    <method-name>methodB</method-name>
  </method>
  <trans-attribute>RequiresNew</trans-attribute>
</container-transaction>

Now let another EJB call methodA with an EJB method call. methodA now runs in an transaction. Will the subsequent call to methodB from methodA run in the same transaction, or does it run in a new transaction? (mind, it's the actual code here. There is no explicit ejb-call to method B)

A: 

They will use the same transaction.

If I remember well, the transaction is started by the container "before" the method is invoked and commited after it "finish".

Since "a" calls "b", "b" would use the same transaction.

:S

I guess the best thing you can do is test it to verify it! :)

OscarRyz
+3  A: 

Your call to methodB() is an ordinary call of a method, not intercepted by the EJB container. Hence both methods will use the same transaction, regardless to what is defined in ejb-jar.xml for calls through ejb interfaces.

david a.
In addition to what david posted, you should read http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Transaction3.html
Kamia