2

I'm new in EJB. I heard that in some cases it is acceptable to store some information (such as configurations) in stateless beans. As I understand the stateless bean lifecycle begin on bean's method invocation and end on method conclusion. After method ends up the stateless bean will be returned to the pool.

  1. I want to know if config field will be reinitialize each time when the bean's method is invoked or only on bean creation.
  2. Also I want to know what else information it is acceptable to store in stateless bean private fields.
@Stateless
public class MyBean {

    private String config = ....;

    //.....
}

Thank you in advance.

MaDa
  • 10,511
  • 9
  • 46
  • 84
VB_
  • 45,112
  • 42
  • 145
  • 293

1 Answers1

2
  1. The private String config = ... will be ininialized only once for every instance of a bean, when it is created (usually during application startup, but also possible when the application server decides that more beans are needed to handle growing traffic).

  2. Basically, when you execute a public bean's method, you're guaranteed to be the sole executor of that bean instance. During that time you're allowed to store anything in private variables. Once you return to the code that called your bean, though, you're never guaranteed that subsequent calls will be directed to the same instance.

Example:

@Stateless
public class MyBean implements MyBeanIntf {

    private Object state;

    @Override 
    public void beanMethod() {
        state = new Object();
        privateMethod();
    }

    private void privateMethod() {
        // it's safe to access 'state' here, will be the one set in
        // beanMethod()
    }

    @Override
    public void otherMethod() {

    }
}

@Stateless
public void MyBeanClient {
    @EJB
    private MyBean myBean;

    someMethod() {
        myBean.beanMethod();
        // Not necessarily the same instance that executed beanMethod
        // will execute otherMethod
        myBean.otherMethod(); 
    }
}

That's theory. In practice, I'd avoid code that relies on keeping internal temporary state in a stateless EJB -- just because this style suggest to other programmers that it's OK to have state of an SLSB in general, which leads to confusing code and potential errors (esp. if state from previous executions is mistakenly picked up as the current one).

MaDa
  • 10,511
  • 9
  • 46
  • 84
  • Thank, it much explain to me. But I don't properly understand in respect to what 'myBean.beanMethod()' can executes on two different beans. As I understand, injection perform only once on MyBeanClient initialization. How is it possible to reinitialize one of it's fields? – VB_ Jun 24 '13 at 07:39
  • 1
    You don't need to reinitialize injected fields - you can think of them as always ready for you. As for the others - you could write an interceptor to do it before every public method call. Check out "EJB interceptors" and "@AroundInvoke" keywords. – MaDa Jun 24 '13 at 07:59
  • Sorry, I asked incorrect question. I mean how is it happens that 'myBean' field (in your example) **changes** the stateless bean instance **on fly**? And thanks for interceptors, never knew about it. – VB_ Jun 24 '13 at 09:05
  • 1
    It doesn't. The trick is elsewhere: the `private MyBean myBean` field is not really of type `MyBean`, but a **proxy** generated by the container, which is compatible with the `MyBean` type. This proxy is responsible for finding the true instance of the bean and passing execution to it. – MaDa Jun 24 '13 at 13:03