4 Replies Latest reply on Mar 2, 2002 2:04 PM by Lars Olsson

    How to find out if two home objects refer to the same bean..

    Lars Olsson Newbie

      Is there a standard way to find out if two variables of
      type EJBHome refer to the same bean?

      I have tried to use EJBHome.equals() but it seems to
      refer true for for EJBHome's that are different:

      EJBHome b1 = (EJBHome)ctx.lookup("B1");
      EJBHome b2 = (EJBHome)ctx.lookup("B2);
      System.err.println(b1.equals(b2));

      This produces 'true' even though b1 and b2 refer to home interfaces of different beans.

      I have tried to compare home handles instead. This
      works to the extent that home handles to homes of
      different beans are seen as different by the method
      HomeHandle.equals(), however, if one looks up a
      home interface twice, and then compare the home handles,
      HomeHandle.equals() returns false!

      EJBHome b1 = (EJBHome)ctx.lookup("B1");
      HomeHandle h1 = b1.getHomeHandle();
      EJBHome b1again = (EJBHome)ctx.lookup("B1");
      HomeHandle h1again = b1again.getHomeHandle();
      System.err.println(h1.equals(h1again));

      This returns false even though both home handles refer
      to the home interface of the same bean.

      I am using Jboss 2.4.4.

      Any help much appreciated, thanks,
      Lars

        • 1. Re: How to find out if two home objects refer to the same be
          Andreas Schouten Newbie

          > I have tried to use EJBHome.equals() but it seems to
          > refer true for for EJBHome's that are different:

          > EJBHome b1 = (EJBHome)ctx.lookup("B1");
          > EJBHome b2 = (EJBHome)ctx.lookup("B2);
          > System.err.println(b1.equals(b2));

          > This produces 'true' even though b1 and b2 refer to home interfaces of different beans.

          Hmmm, b1 and b2 should be instances of different classes (i.e. B1Home and B2Home)
          Check, if b1.getClass().getName() is the same as b2.getClass().getName() (I assume it's proxy.)

          If not B1Home.class.isInstance(b1) and B2Home.class.isInstance(b1), check Your deployment descriptor. Maybe B1 and B2 indeed assignd to the same home interface.

          • 2. Re: How to find out if two home objects refer to the same be
            Chris Huang Newbie

            try the followings:

            EJBHome b1 = (EJBHome)ctx.lookup("B");
            EJBHome b2 = (EJBHome)ctx.lookup("B");

            EJBObject e1 = b1.findByPrimaryKey(k1);

            EJBObject e2 = b1.findByPrimaryKey(k1);

            //e1 and e2 should be the same bean

            • 3. Re: How to find out if two home objects refer to the same be
              Lars Olsson Newbie

              > Maybe B1 and B2 indeed assignd to the same home interface.

              No - here is another test program:

              EJBHome h1 =(EJBHome)ctx.lookup("wis/ejb/boxmgr");
              Class h1class = h1.getClass();
              System.out.println("h1=" + h1 + ", h1class=" +h1class.getName());

              EJBHome h2 = (EJBHome)ctx.lookup("wis/ejb/usermanager");
              Class h2class = h2.getClass();
              System.out.println("h2=" + h2 + ", h2class=" + h2class.getName());

              boolean h1_is_h2class = h2class.isInstance(h1);
              System.out.println("(h1 instanceof h2class) = " + h1_is_h2class);

              boolean h2_is_h1class = h1class.isInstance(h2);
              System.out.println("(h2 instanceof h1class) = " + h2_is_h1class);

              boolean eq = h1.equals(h2);
              System.out.println("(h1==h2) = " + eq);

              and here is the output:

              h1=wis/ejb/boxmgrHome, h1class=$Proxy0
              h2=wis/ejb/usermanagerHome, h2class=$Proxy1
              (h1 instanceof h2class) = false
              (h2 instanceof h1class) = false
              (h1==h2) = true

              • 4. Re: How to find out if two home objects refer to the same be
                Lars Olsson Newbie

                > EJBObject e1 = b1.findByPrimaryKey(k1);
                > EJBObject e2 = b1.findByPrimaryKey(k1);

                My beans are stateless session beans so I assume
                the findByPrimaryKey() operation cannot be used.

                I could of course use introspection to find the
                create() method and call that to create the beans
                to be able to perform the comparison.

                Lars