Consider the following two EJBs:
@Stateless
public class MyBean1 {
pulic void method1() {
//method implementation comes here
}
pulic void method2() {
//method implementation comes here
}
}
@Stateless
public class MyBean2 {
@EJB
MyBean1 myBean1;
public void businessMethod() {
myBean1.method1();
myBean1.method2();
}
}
Will the container gurantee that the two methods are called on the same instance of MyBean1 (and no other methods are called in between the two method calls on that instance)?
You can't tell if you get access to the same instance or not. The container decides about that.
Even if you "think" it's the same instance (e.g. one @EJB MyBean1
in your code) those calls might hit different MyBean
instances.
Other business methods of your EJB might be called between your myBean1.method1()
and myBean1.method2()
invocations. This might be e.g. a call to the EJB made from different component.
This should, however, not interest you so much because you're using stateless EJB's - i.e. you shouldn't store any state in your EJB.
Therefore, either you get the same or different EJB instance or some other component will invoke a business method on the EJB of the same type between your calls should not be a concern.