3 Replies Latest reply on Oct 9, 2003 2:45 AM by o_milton

    setEntityContext not being called

    darranl

      Hi,

      I am using JBoss 3.2.1 with Suns JDK '1.4.1_03'.

      I am experiencing a problem where the method setEntityContext is not always being called correctly.

      In my test case I have a simple CMP entity bean with two properties and a session bean to access the entity bean.

      I first run a client to call 'initialise' on the session bean, this creates three different instances of the entity bean and the following messages are displayed :-

      10:55:26,702 INFO [STDOUT] setEntityContext
      10:55:26,712 INFO [STDOUT] setEntityContext
      10:55:26,712 INFO [STDOUT] setEntityContext


      I then run a different client to call testBeans on the session bean and the following messages are displayed :-

      10:55:33,783 INFO [STDOUT] setEntityContext
      10:55:33,793 INFO [STDOUT] Bean Fount = 'Test 2', Test Bean 2
      10:55:33,793 INFO [STDOUT] This instance is 'Test 2'
      10:55:33,793 INFO [STDOUT] The local interface is for 'Test 2,


      I then wait for 15 minuted and run the client that calls testBeans again, this time the following messages are displayed :-

      11:11:37,498 INFO [STDOUT] Bean Fount = 'Test 2', Test Bean 2
      11:11:37,498 INFO [STDOUT] This instance is 'Test 2'
      11:11:37,498 INFO [STDOUT] The local interface is for 'Test 3,


      This time setEntityContext is not called so when I try and obtain the local interface to the bean I end up with the local interface to a different bean.

      Has anyone else experienced anything similar to this?

      Darran.

      For some reason the attach files button does not appear to be working so I have pasted the beans below :-

      TestEntityBean

      package com.darranl.test.implementation;

      import java.rmi.RemoteException;

      import javax.ejb.CreateException;
      import javax.ejb.EJBException;
      import javax.ejb.EntityBean;
      import javax.ejb.EntityContext;
      import javax.ejb.RemoveException;

      import com.darranl.test.interfaces.TestEntityLocal;

      /**
      * @ejb:bean
      * name = "TestEntity"
      * jndi-name = "TestEntity"
      * reentrant="true"
      * primkey-field="name"
      *
      * @ejb:persistence
      * table-name="tbl_testentity"
      *
      * @ejb:pk
      * class="java.lang.String"
      * generate="false"
      *
      * @jboss:persistence
      * row-locking="true"
      *
      * @ejb.finder
      * signature="java.util.Collection findAll()"
      */
      public abstract class TestEntityBean implements EntityBean {

      private EntityContext context = null;

      /**
      * @ejb:create-method
      *
      * @param name
      * @return String
      * @throws CreateException
      */
      public String ejbCreate(String name) throws CreateException {
      setName(name);
      return null;
      }

      /**
      * @ejb:persistence
      * column-name="name"
      *
      * @ejb:interface-method
      */
      public abstract String getName();

      public abstract void setName(String name);

      /**
      * @ejb:persistence
      * column-name="comment"
      *
      * @ejb:interface-method
      */
      public abstract String getComment();

      /**
      * @ejb:interface-method
      *
      * @param comment
      */
      public abstract void setComment(String comment);

      /**
      * @ejb:interface-method
      */
      public void testInterface() {
      System.out.println("This instance is '" + getName() + "'");
      TestEntityLocal local = (TestEntityLocal) context.getEJBLocalObject();
      System.out.println("The local interface is for '" + local.getName() + ",");
      }

      /**
      * @see javax.ejb.EntityBean#ejbActivate()
      */
      public void ejbActivate() throws EJBException, RemoteException {
      }

      /**
      * @see javax.ejb.EntityBean#ejbLoad()
      */
      public void ejbLoad() throws EJBException, RemoteException {
      }

      /**
      * @see javax.ejb.EntityBean#ejbPassivate()
      */
      public void ejbPassivate() throws EJBException, RemoteException {
      }

      /**
      * @see javax.ejb.EntityBean#ejbRemove()
      */
      public void ejbRemove()
      throws RemoveException, EJBException, RemoteException {
      }

      /**
      * @see javax.ejb.EntityBean#ejbStore()
      */
      public void ejbStore() throws EJBException, RemoteException {
      }

      /**
      * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
      */
      public void setEntityContext(EntityContext context)
      throws EJBException, RemoteException {
      System.out.println("setEntityContext");
      this.context = context;
      }

      /**
      * @see javax.ejb.EntityBean#unsetEntityContext()
      */
      public void unsetEntityContext() throws EJBException, RemoteException {
      System.out.println("unsedEntityContext");
      this.context = null;
      }

      }

      TestSessionBean

      package com.darranl.test.implementation;

      import java.rmi.RemoteException;

      import javax.ejb.CreateException;
      import javax.ejb.EJBException;
      import javax.ejb.FinderException;
      import javax.ejb.SessionBean;
      import javax.ejb.SessionContext;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;

      import com.darranl.test.interfaces.TestEntityLocal;
      import com.darranl.test.interfaces.TestEntityLocalHome;

      /**
      * @ejb:bean
      * name = "TestSessionBean"
      * type = "Stateless"
      * jndi-name = "TestSessionBean"
      */
      public class TestSessionBean implements SessionBean {

      /**
      * @ejb:create-method
      *
      * @throws CreateException
      */
      public void ejbCreate() throws CreateException {
      }

      /**
      * @ejb:interface-method
      */
      public void initialise() {
      try {
      Context ctx = new InitialContext();
      TestEntityLocalHome telh =
      (TestEntityLocalHome) ctx.lookup("TestEntityLocal");

      TestEntityLocal local1 = telh.create("Test 1");
      local1.setComment("Test Bean 1");

      TestEntityLocal local2 = telh.create("Test 2");
      local2.setComment("Test Bean 2");

      TestEntityLocal local3 = telh.create("Test 3");
      local3.setComment("Test Bean 3");

      } catch (NamingException e) {
      e.printStackTrace();
      } catch (CreateException e) {
      e.printStackTrace();
      }

      }

      /**
      * @ejb:interface-method
      */
      public void testBeans() {

      try {
      Context ctx = new InitialContext();
      TestEntityLocalHome telh =
      (TestEntityLocalHome) ctx.lookup("TestEntityLocal");

      TestEntityLocal local2 = telh.findByPrimaryKey("Test 2");

      System.out.println(
      "Bean Fount = '" + local2.getName() + "', " + local2.getComment());

      local2.testInterface();

      } catch (NamingException e) {
      e.printStackTrace();
      } catch (FinderException e) {
      e.printStackTrace();
      }

      }

      /**
      * @see javax.ejb.SessionBean#ejbActivate()
      */
      public void ejbActivate() throws EJBException, RemoteException {
      }

      /**
      * @see javax.ejb.SessionBean#ejbPassivate()
      */
      public void ejbPassivate() throws EJBException, RemoteException {
      }

      /**
      * @see javax.ejb.SessionBean#ejbRemove()
      */
      public void ejbRemove() throws EJBException, RemoteException {
      }

      /**
      * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
      */
      public void setSessionContext(SessionContext arg0)
      throws EJBException, RemoteException {
      }

      }

        • 1. Re: setEntityContext not being called
          scoy

          Hi,

          There are two unrelated things happening here.

          1. You need to review the lifecycle of an entity bean to understand when setEntityContext is called;

          2. EntityContext.getEJBLocalObject is very broken in JBoss 3.2.1. This is the source of your incorrect behaviour. You need JBoss 3.2.2RC2 (I think) or newer to overcome this problem.

          Steve Coy

          • 2. Re: setEntityContext not being called
            darranl

            Steve,

            Thanks for your reply.

            Yes I understand that how often setEntityContext is called can vary depending on a number of factors, my concern was that the context set in my bean was referring to incorrect values.

            I will download JBoss 3.2.2RC2 to see if that works better.

            Thanks again,
            Darran.

            • 3. Re: setEntityContext not being called
              o_milton

              Occasionally I get the wrong principal from the context, i.e., a principal who made a call to this instance earlier. I wait for the 3.2.2 release before I dig deeper into this problem. Can anyone confirm my suspicions?