Discussions

EJB design: can self call start transaction in stateless session bean

  1. 1. Is it possible to associate declarative transcation attributes to a private method in a stateless session bean.

    2. Will a call from one method (which is not associated with a transaction) to another method (which has transaction attribute required) of the same statless session bean start a transaction?
  2. Starting Transaction[ Go to top ]

    You can not start transaction, unless you explicitly work with UserTransaction object. It is a Container's responcibility to start transactions on method invocation in EJB's, since by the time you are in your Class instance the transaction has not been started, and you will simply call a method without involving any of the container's services; transaction will not start. You could start a transaction by using java transaction API. I mean looking up UserTransaction object and doing begin, (comit | rollback) explicitly.

    I hope I am making sense.

    Later, Mikhail
  3. Answer 1) Only public methods that are including in the Remote or Local interface of your EJB can have declaritive transaction attributes. After all, these are the only methods you can put in your Deployment Descriptor (ejb-jar.xml).

    Answer 2) It depends on how you make the method call. Assuming you are talking about methods in the same session bean, there are two possiblities.

    A) Method 1 calls Method 2 directly. No transaction will start.

    B) Method 1 using JNDI and the Home interface to look up the session EJB and invokes it the same way that an EJB client would. In this case, a transaction WILL start.

    Declaration transactions only work when methods are invoked through the Remote/Local interface, and therefore pass through the EJB stub layers generated by the server that control transactions.