Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reference to component or home interface in session facade? Which one is better?  RSS feed

 
Alena Al
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Morning all,

Maybe it is not the most intelligent question but please, please answer !!! I need to ask since I was looking through specification, documentation, etc. and it did not become any more clear to me in terms of a good design.


I have a Stateful Session Bean that is my session facade. It manages two other Stateful Session Beans. Is it better
1 case. keep reference to component interfaces (EJBObject or Address1 & Address2 in my example) or
2 case. keep references to home interfaces (EJBHome or AddressHome1 & AddressHome2 in my example) and to create EjbObject every time there is a need to mange those beans?

I work in a clustered environment with non-sticky approach so requests got load-balances between nodes.

here is an example of cases (Address1 & Address2 is just for the example, never mind the names)


1 case:
AddressManager{
protected Address1 address = null;
protected Address2 address = null;

public void ejbCreate() throws CreateException {
AddressHome addressHome1 = (AddressHome1)ServiceLocator.getInstance().getRemoteHome("Address1", AddressHome1.class);
address1 = addressHome1.create();

AddressHome addressHome2 = (AddressHome2)ServiceLocator.getInstance().getRemoteHome("Address2", AddressHome2.class);
address2 = addressHome2.create();
........
}

2 case:

AddressManager{
protected AddressHome1 addressHome1 = null;
protected AddressHome2 addressHome2 = null;

public void ejbCreate() throws CreateException {
addressHome1 = (AddressHome)ServiceLocator.getInstance().getRemoteHome("Address1", AddressHome1.class);

addressHome2 = (AddressHome)ServiceLocator.getInstance().getRemoteHome("Address2", AddressHome2.class);
}
public doSomething(){
Address1 address1 = addressHome1.create();
Address2 address2 = addressHome2.create();
do something with address1 & address2 beans
}
}

It is easier of course to keep references to component interfaces since creating address1 & address2 for every request would take time but my main concern is - is it safe in cluster?

Would it be any different if session facade manages two Entity Beans instead of two Session Beans?


Thanks in advance
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which application server are you using?
This article gives details about behavior of ejbs in a weblogic cluster.
Basically, both ejb home and ejb object can be cluster aware.
Ejb object state is replicated over all the cluster nodes. So, you can seamlessly call any ejb object reference without worrying about which cluster node does the call ultimately land into.
So, either approaches you are safe in the clustered environment (If using weblogic you have to specify in the descriptor that your home interface is cluster aware)
[ July 17, 2007: Message edited by: Nitesh Kant ]
 
Alena Al
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the prompt answer. I am developing for JBoss but keep in mind that later on we might possible change the server. I looked it up in JBoss documentation and I did not really find them saying that they implemented ejb object as being cluster aware but they probably are.

I have another question though.

I tried to keep references to local home interfaces and it seems to work even if the requests are balanced. This is something I do not understand. I thought that local home reference will be invalid on the node where it was not originally lookup up at.

I tried the following.

0. I start JBoss. Beans are defined as <clustered>true. I have 2 nodes. Node1 & node2/
1. I send request. Local Home Reference is looked up at node1 and kept as instance variable of Session Facade as shown below
public class AddressManagerBean implements SessionBean {
protected WorkAddressLocalHome workAddressHome = null;
public void ejbCreate{
workAddressHome = (WorkAddressLocalHome)ServiceLocator.getInstance().getLocalHome("WorkAddressLocal");
}
.......
}

2. I kill node1
3. I send request again. I goes to node2. My facade gets the and executed doSomething and it works
public doSomething(){
WorkAddressLocal bean = workAddressHome.create();
bean.testSomething();
}

So WHY does it work?

I guess I should ask this question on JBoss forum but if anyone can answer here I would be most grateful
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!