Discussions

EJB programming & troubleshooting: Why does the spec advice against passing instance variables?

  1. Can anybody tell me why the EJB 2.1 spec advice against passing contents of instance variables as arguments of local interface (see except below)?

    10.3.9 Non-persistent state

    The Bean Provider should exercise care in passing the contents of instance variables as the arguments or
    results of method invocations when local interfaces are used. In general, the Bean Provider should avoid
    passing state that is maintained in instance variables as the argument or result of a local method invocation
  2. As per EJB Spec,Remote interfaces pass parameters using call-by-value semantics, while local interfaces use call-by-reference.
    Using local interfaces it is possible for beans to expose their state and use pass-by-reference to pass their state between related bean instances
    But if we change this deployment scenario in to a remote interface based design due to new requirements or some other reason, all the state-passing logic mentioned above goes hay-wire. They will not work anymore.Changes to implementation code would not be simple.

    So one needs to consider such scenarios upfront and make safe decisions as to which can be represanted via local interfaces(for ever)
    HTH
  3. Yes the explaination seems to be correct except the argument that 'Using local interfaces it is possible for beans to expose their state and use pass-by-reference to pass their state between related bean instances'. The above statement is true but nobody will be willing to pass states between bean instances which completely destroys the purpose of EJBs which should be immutated instances when in pool. Hence the possible explaination for the specs not allowing passing instance variables to method arguments or return values could be because it can cause the change in state of beanInstances .

    Cheers