Creating Session Beans in Session Beans


EJB programming & troubleshooting: Creating Session Beans in Session Beans

  1. Creating Session Beans in Session Beans (6 messages)

    Hi all,

    I'm new to EJB's and was wondering if it is possible to create a session bean as a variable in another session bean, and if so how? It is for my final year project which is due in two weeks, so I'd really appreciate any help.

    What I am trying to do is have a session bean, representing a Bank Clerk and another session bean which represents a Security manager. I want an instance of the security manager to be a variable in the Clerk Bean.

    Like I said, I'd really appreciate any help.

  2. Yes, the bean can look up the reference from JNDI just like any other client.

    Just beware of what happens when the bean is passivated. In theory, the server should take of the reference for you, but it is generally better practise to release the reference in ejbPassivate() and restore it in ejbActivate().


  3. Hi Tony,
    Thanks for your reply. I've tried setting it up just like I would in a normal client.

    I use the following code:

     public Object getHome(String _name, Class _type){
                Object ref = jndiContext.lookup ("java:comp/env/ejb/" + _name);
                return PortableRemoteObject.narrow(ref,_type);
            }catch(NamingException ne){
                throw new EJBException(ne);

    and then
       SecurityManagerHome home = (SecurityManagerHome)getHome("SecurityManagerHome",SecurityManagerHome.class);
              SecurityManager sec = home.create();
            }catch(Exception e){

    I keep getting the following error in my Jboss server when I deploy the Clerk bean:

     java.lang.NoClassDefFoundError: ca4/securebank/server/ejb/SecurityManager

    even though it is deployed and the package is correct.

    Any ideas please?
    Once again, thanks for your help
  4. Could be a bug in JBOSS. Try adding the relevant classes to the normal classpath (rather than relying on them to be served out of the bean JAR file.)


  5. Hi,
    Setting the classpath explicitly doesn't seem to work. It now appears that it can't even find the home interface of the bean I'm trying to get!

    The error is:
    javax.naming.CommunicationException. Root exception is [Verifier] java.lang.ClassNotFoundException: ca4.securebank.server.ejb.SecurityManagerHome

    I can acces the beans from a client application, but not from other EJB's.

  6. Check the Global JNDI Namespace with the following:


    to verify if you are acessing your EJBs correctly. Then you would know if you should be using "SecurityManager" instead of "SecurityManagerHome" in the getHome() function.
  7. The problem seems to be as follows:

    If you have two session beans packaged in two different deployment jars with their respectively deployment descriptors, the classes in the two EJBs are loaded by two different ejb classloaders. So even if everything else is right, the classes in one EJB are not visible to classes in the other.
    So the solution is to create an ear file containing the two deployment jars and place it in the applications sub directory of the server. So when the WLS starts it deploys the EJBs whose deployment jars are available in the applications sub directory. If an .ear file is available for deployment, WLS uses a single classloader to load all the class files in the two jar files in the .ear file. So this problem will not arise.
    So if u have two deployment jars as deploy_Bean1.jar and deploy_Bean2.jar corresponding to the two EJBs, then
    jar cvf deploy_Bean.jar deploy_Bean1.jar deploy_Bean2.jar will do the trick.
    Please try this out.