Yes you can cache the home interfaces; but not the beans because the methods on the home interfaces may have different permissions depending on which user is making the call.
home.create() is not very expensive anyway.
On your second question jboss by default also starts a naming service. So if you don't change anything, in your example you would have 2 servers and you might configure their names in a properties file leaving near your servlets. In a more elaborate system you could of course imagine the server settings (I mean naming-hostname & port) coming from some dynamic source such as an ldap repository.
Thanks for your reply. I am not sure what you mean in the second paragraph. Are you saying that JSP pages get one of the naming service running on Machine C and D from either a property file or from a central ldap and then look up the beans it needs to access? If this is the case, you can't really cache home interface. In addition, if you deploy HelloBean on machine C and AnotherBean on machine D, do you have to put this info in the property file or ldap that you have to look up HelloBean from Machine C naming service? Is it possible to configure a central naming service which has all the beans' info in JBoss? If we have to use an external naming server, how do we register ejbs with it? Thanks.
Get and read the jboss 3 clustering docs. It has clustered jndi.
A couple of points. Hello is not a proxy to your bean, but technically is a proxy to the EJBObject. You could write a neat class that caches the context and home objects for you. Then your code becomes something like:
Hello bean = (Hello)MyCache.getBean ("Hello", Hello.class)
If you are using JBoss, then such a class is very easy to write. But if you use some other app server, then you may have to write more code, to handle what happens if the connection to Jndi goes down.
Interesting. Are you saying that you don't need to handle the case for JBoss? Is it because bean and jndi lives in the same JVM? How do you know the connection with jndi is dropped anyway? Can you simply determine simply based on you catch some exception when doing any of the following:
What should you do in this case? Do another lookup?
First of all, follow David's advice and read docs on Jboss clustering and all will become clear. In JBoss, the reference to the context is a lot more than a simple reference to some remote object. In JBoss, there is a lot of intelligence built into that proxy that will reestablish connections. Read the docs !! Absent using a JBoss server, a simple procedure to follow would be:
ask home for EJBObject ref
if failure, then ask context for new ref to home and go to top
if failure, then get new context and recycle
Of course, build in logic to avoid infinite loop.
There is more you could do based on type of exceptions thrown, but hey, use the superior JBoss, and its all handled for you.