Discussions

EJB programming & troubleshooting: Returning more than one value/object from an EJB method

  1. Hi,
    I have the follong method in the remote interface of my EJB:

    void createSomeObject( MyObject obj1, List returnList1, Map returnMap1);

    Since i want to return a List (returnList1) and a Map(returnMap1) from my EJB method "createSomeObjects", i am passing these as parameters thinking they will be passed by reference and the client(servlet) gets the changes the EJB made to these objects.

    But it doesn't seem to be working, the EJB is filling the List and Map objects and the servlet doesn't get it (pass by value problem).

    My understading is that, all parameters to an EJBs are passed by value(because they are remote calls, the RMI thing), let me know if this right.

    Is there any workaround apart from the following alternative?

    Alternatives:
    1. Wrap "List list1" and "Map map1" in another model and return this instead of passing them as parameters.

    Thanks
    Vasu
  2. Vasu,

    I beleive most of the application servers have an ability to turn on passing parameters by reference if a client and EJB reside in the same application (ejb jar or ear). If it's not the case, returning a compound result object you mentioned is the only option.

    Hope this helps.

    Regards,

    Slava Imeshev
  3. I beleive most of the application servers have an ability to turn on passing parameters by reference if a client and EJB reside in the same application (ejb jar or ear).

    This is exactly the thing that should be avoided. It's quite questionable even for "plain" Java to use such kind of trick -- one element array as parameter, object holder as parameter. Slava, do you remember that long flame in fido7.ru.java on this issue (I believe you was there :-)?

    BTW, I have experienced a noisy side effect of the "feature" you mentioned and provided by JBoss. I work with java.util.Date object and instead of creating new one to store current time value in SLSB code I've just assigned a value to parameter. Then I continue to use value (actually, reference) passed as parameter in caller code. Got it? It took certain time to detect the source of the bug.

    If it's not the case, returning a compound result object you mentioned is the only option.

    I'd prefer to say "the only valid & portable & non-intrusive for others" approach.

    P.S. Just for fun (I know the answer). I can memorize the connected question: how to pass callback in EJB method? Any thoughts?

    VS
  4. I think it all depends. Java is build around idea of passing parameters by reference. Passing by value comes from unification of access for remote objects. If you don't play with threads, passing by reference in J2EE environment can bring in noticable performance increase. I'm not advocating PBR blindly - if you don't have bottlenecks connected caused by serialization, it may make sense to use "normal" PBV approach.