aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Passing parameters between Session beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Passing parameters between Session beans" Watch "Passing parameters between Session beans" New topic
Author

Passing parameters between Session beans

Harish Vaishnav
Greenhorn

Joined: Mar 17, 2006
Posts: 3
Hello folks - working on my first J2EE project now.

I am trying to implement exception-handling with a facility to show detailed and informative error message to the user. We are using Struts. I am instantiating a simple Java bean (public class, implementing Serializable) from the ActionClass. I am passing this object on to the business delegate (stateless session) bean.

The delegate, in turn, passes this to a session facade (stateless session) bean.

The facade, in trun, passes this error bean to a DAO object, where lots of action takes place.

The idea is, at any point, when a exception is thrown, I will set a code into this error bean, and continue to normally propogate the exception using throw. I expect this to happen till I reach the ActionClass, where I will examine the bean, get the code, and fetch a nice message from a property file.

An error is simulated in DAO code; this causes the error object to be filled with a error code; this code is also visible to the Session facade bean (because objects are passed by reference). However, the business delegate is unable to get this code, in fact, if I preset the error object with some junk code in the business delegate before passing the object to the facade, it continues to hold this value even after returning from the facade!

Does this mean, between two session beans, we do not have reference passing of objects? Between session beans, is the mechanism always pass-by-value even for entire objects?

Else, what could be wrong? Please suggest. Thanks for reading this longish post, and thanks for any help.

Also pls suggest how this approach can be further improved.

Regards/Harish
[ March 17, 2006: Message edited by: Harish Vaishnav ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10033
    
163

Instead of passing on error code related information as arguments to methods, a better approach would be to throw an application exception from the session beans on encountering an error. This application exception would contain the error code. One question to you, do you expect the transaction to be rolled back when an error occurs? If yes, then in the approach that you have stated, i dont see a way this can be done.


[My Blog] [JavaRanch Journal]
Harish Vaishnav
Greenhorn

Joined: Mar 17, 2006
Posts: 3
jaikiran pai:

Thanks for the response.

- How would the application exception contain the error code?

- Since I am using a custom error object, which contains the error code, I can afford to only fill up this object and then always throw new EJBException(e), which I believe causes the container to rollback.

So, by using a simple error object, I plan to give a better user response without losing container tran management. But I am facing the problem I described earlier.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10033
    
163

Since I am using a custom error object, which contains the error code, I can afford to only fill up this object and then always throw new EJBException(e)


This means that you dont need the pass the error object as part of method parameters. Whenever an error occurs and the EJBException is thrown, you then retrieve your custom error object(that you wrapped inside the EJBException, as EJBException(e)) from this EJBException instance by invoking the getCause() method on the same.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10033
    
163

Something like:

Harish Vaishnav
Greenhorn

Joined: Mar 17, 2006
Posts: 3
jaikiran pai:

My situation is like this: there is a need to present detailed message to the user. We have decided that instead of defining custom exceptions extending java.lang.Exception, we will create a small class instead:

public class CustomError {
int eCode;
String eMsg;
String userAction;
...
...
}

We will pass this class's object along all the way from ActionClass to the DAO layer where, potentially, this object will be filled with a error code. (If this object were to be filled, we will also have come out of any trans due to "throw new EJBException();")

So, we are not subclassing Exception at all in this approach, relying entirely on java's default "pass-by-reference" behavior for objects to retrieve it back in the ActionClass for further action.

This expected java "pass-by-reference" behavior is failing at the business delegate, and I cannot get back the same object I set into circulation from the ActionClass. Instead, I am stuck with the object originally sent into the delegate from the ActionClass.

Hope you understand my problem.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6


I am passing this object on to the business delegate (stateless session) bean.

The delegate, in turn, passes this to a session facade (stateless session) bean.


You shouldn't need to go through two EJB Session Beans here. In the Struts best practice architecture the Business Delegate would not be a Stateless Session bean but just a POJO.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing parameters between Session beans