0

Suppose we have 2 EJB's (stateless for example) A and B.

@Stateless
public class A {
    @EJB
    protected B binstance;
}

So after we used our A bean in servlet, it should be returned back to pool, and the question is does EJB container "dereference" B instance from A, i.e. at the next invocation we could get another B object inside the same A instance? Is the same still valid for JAX-RS, when we make a bean REST service and inject some other beans there? Does JAX-RS bean created like servlet or like an ordinary EJB instance in this sense (I mean servlets still hold references to beans instances)?

user3070377
  • 441
  • 4
  • 10
  • possible duplicate of [how is state of instance variables of a stateless bean preserved for next invocation in EJB?](http://stackoverflow.com/questions/26309050/how-is-state-of-instance-variables-of-a-stateless-bean-preserved-for-next-invoca) – kolossus Sep 01 '15 at 18:58
  • JAX-RS resources cannot be pooled per se. Since Java EE 7 (and EJB 3.2) if you combine JAX-RS with EJB (i.e. annotate a JAX-RS resource with @Stateless) the JAX-RS will be pooled. All its dependendies will be wired ad hoc when it is loaded from the pool (likewise for all the stateless injected EJBs). – Mike Argyriou Sep 03 '15 at 09:55
  • The question is what happens to B before A is placed in the pool? Is B dereferenced from A, and both A and B pooled independently, or only A is returned to the pool (as it has reference to B, so how could B pooled then?) – user3070377 Sep 06 '15 at 21:02

1 Answers1

0

Ok. So let's start with stateless beans. In case of stateless beans objects pool is controlled by bean container. And it may provide same object in first and second request or may provide different objects. So while coding, keep this in mind.

Now coming to Bean A and Bean B, when bean A execution is completed, Bean B may get dereferenced. As this is purely determined by Bean container based on load and how the duration between bean A calls. So never depend on,that you will get same Bean B object everytime.

And in case of JAX-RS, rest instances are created like ordinary EJB instances. If rest bean is stateless, apply stateless bean rules to it. In this case also you will have no control over instances provided to you in adjutant rest calls.