SessionContext.getBusinessObject () in search of EJB3 and JNDI

In EJB2, you must use the getEJBBusinessObject () method in EJB to pass a reference to itself when calling another (local / remote) bean.

Does the same apply to EJB3 ?

eg.

@Stateless public class MyBean implements MyBeanLocal { @Resource private SessionContext sessionContext; public void myMethod() { OtherBeanLocal otherBean = ...; // getting reference to other local EJB. MyBeanLocal myBean = sessionContext.getBusinessObject(MyBeanLocal.class); b.aMethod(myBean); } // Edit: calling myMethodTwo() from inside of myMethodOne() public void myMethodOne() { MyBeanLocal myBean = sessionContext.getBusinessObject(MyBeanLocal.class); myBean.myMethodTwo(); } public void myMethodTwo() { ... } ... } 

Also, if I retrieve my local bean method using the getBusinessObject () method, is it the same as if I used a generic JNDI lookup?

I tested both approaches and both work, but I'm not sure if the bean is handled in the same way by the container.

Edit : Does ejb itself select a link when calling myMethodTwo () from myMethodOne () of the same ejb, is it still needed in EJB3? Is it allowed to call methods inside the same ejb through this link? How will these transactions be handled if I decide to use some?

+4
source share
1 answer

Yes, the same applies to EJB 3. Yes, getBusinessObject is an analog of EJB 3 getEJBObject (or getEJBLocalObject). All of these methods return a proxy for the current bean. For a stateless session, beans is basically the same as looking at JNDI, although it will probably work better as it avoids the overhead of JNDI.

+5
source

All Articles