6 Replies Latest reply on Nov 16, 2007 10:53 PM by satan3

    javax.naming.NamingException: Could not dereference object -

    htran_888

      Hi All,

      I am receiving the following error when trying to reference 2 Stateful Session beans (TransactionPersistenceContext & ExtendedPersistenceContext):

      -------------------------------------------------------------------------
      Distributing D:\Test\EJB3ex511-client\dist\EJB3ex511-client.jar to [org.jboss.deployment.spi.LocalhostTarget@13aaba1]
      Deploying D:\Test\EJB3ex511-client\dist\EJB3ex511-client.jar
      Deploying D:\Test\EJB3ex511-client\dist\EJB3ex511-client.jar
      Applicaton Deployed
      Operation start started
      Operation start completed
      run-deploy:
      run-tool:
      run-jar:
      log4j:WARN No appenders could be found for logger (org.jboss.security.SecurityAssociation).
      log4j:WARN Please initialize the log4j system properly.
      no cabin should be null: null
      Master Suite
      1
      1
      3
      Updating detached cabin instance with new bed count of 4
      Finding cabin to see it has been updated with a merge() on server
      new bed count is: 4
      javax.naming.NamingException: Could not dereference object [Root exception is java.lang.reflect.UndeclaredThrowableException]
      at org.jnp.interfaces.NamingContext.getObjectInstanceWrapFailure(NamingContext.java:1150)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:705)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
      at javax.naming.InitialContext.lookup(InitialContext.java:392)
      at ejb3ex511client.Main.main(Main.java:50)
      Caused by: java.lang.reflect.UndeclaredThrowableException
      at $Proxy1.createProxy(Unknown Source)
      at org.jboss.ejb3.JndiProxyFactory.getObjectInstance(JndiProxyFactory.java:52)
      -------------------------------------------------------------------------------
      Here are the codes for 1 Stateless (TravelAgentBean - pre-requisite) & 1 Stateful (TransactionPersistenceContextBean) session beans and a Client manipulating these beans:

      @Stateless
      public class TravelAgentBean implements TravelAgentRemote
      {
       @PersistenceUnit(unitName="EJB3ex51") private EntityManagerFactory factory;
       @PersistenceContext(unitName="EJB3ex51") private EntityManager manager;
      
       public void createCabin(Cabin cabin)
       {
       manager.persist(cabin);
       }
      
       public Cabin findCabin(int pKey)
       {
       return manager.find(Cabin.class, pKey);
       }
      
       public void updateCabin(Cabin cabin)
       {
       manager.merge(cabin);
       }



      @Stateful
      public class TransactionPersistenceContextBean implements TransactionPersistenceContextRemote
      {
       @PersistenceContext(unitName="titan", type=PersistenceContextType.TRANSACTION)
       private EntityManager manager;
      
       private Cabin cabin;
      
       public void setCabin(int pk)
       {
       cabin = manager.find(Cabin.class, pk);
       }
      
       public void updateBedCount(int newBedCount)
       {
       cabin.setBedCount(newBedCount);
       }
      
       @Remove
       public void remove()
       {
       }
      }


      public static void main(String[] args) {
      
       try
       {
       Context jndiContext = getInitialContext();
       Object ref = jndiContext.lookup("travelagent.TravelAgentRemote");
       TravelAgentRemote dao = (TravelAgentRemote)ref;
      
       Cabin noCabin = dao.findCabin(1);
       System.out.println("no cabin should be null: " + noCabin);
      
       Cabin cabin_1 = new Cabin();
       cabin_1.setId(1);
       cabin_1.setName("Master Suite");
       cabin_1.setDeckLevel(1);
       cabin_1.setShipId(1);
       cabin_1.setBedCount(3);
      
       dao.createCabin(cabin_1);
      
       Cabin cabin_2 = dao.findCabin(1);
       System.out.println(cabin_2.getName());
       System.out.println(cabin_2.getDeckLevel());
       System.out.println(cabin_2.getShipId());
       System.out.println(cabin_2.getBedCount());
      
       System.out.println("Updating detached cabin instance with new bed count of 4");
       cabin_2.setBedCount(4);
       dao.updateCabin(cabin_2);
      
       System.out.println("Finding cabin to see it has been updated with a merge() on server");
       Cabin cabin_3 = dao.findCabin(1);
       System.out.println("new bed count is: " + cabin_3.getBedCount());
      
       ref = jndiContext.lookup("travelagent.TransactionPersistenceContextRemote");
       TransactionPersistenceContextRemote txBean = (TransactionPersistenceContextRemote)ref;
      
       Cabin fetchedCabin = dao.findCabin(1);
       int oldBedCount = fetchedCabin.getBedCount();
      
       System.out.println("Set up transaction persistence context stateful bean");
       txBean.setCabin(1);
       txBean.updateBedCount(5);
      
       fetchedCabin = dao.findCabin(1);
       System.out.println("Cabin bed count will still be " + oldBedCount + ": " + fetchedCabin.getBedCount());
      
       System.out.println("Set up extended persistence context stateful bean");
      
       ref = jndiContext.lookup("travelagent.ExtendedPersistenceContextRemote");
       ExtendedPersistenceContextRemote extendedBean = (ExtendedPersistenceContextRemote)ref;
      
       extendedBean.setCabin(1);
       extendedBean.updateBedCount(5);
      
       fetchedCabin = dao.findCabin(1);
       System.out.println("Cabin bed count will be 5: " + fetchedCabin.getBedCount());
      
       // cleanup
       txBean.remove();
       extendedBean.remove();
      
       }
       catch (javax.naming.NamingException ne)
       {
       ne.printStackTrace();
       }
       }
      
       public static Context getInitialContext()
       throws javax.naming.NamingException
       {
       return new javax.naming.InitialContext();
       }


      I have tried to split the client into separate classes but the outcome is still the same.

      I am running Netbeans 5.5, JBoss 4.0.5 AS on Windows XP platform.

      I have struggled with this issue for many days after trying different method of deployment including using both sun-appserver-pe9.0 & JBoss 4.0.5 AS without success.

      This exercise is from workbook (ex05_1), title EJB 3.0 by Bill Burke.

      Any assistance would be very much appreciated.

      Thanks,

      Henry

        • 1. Re: javax.naming.NamingException: Could not dereference obje
          htran_888

          Hi,

          I managed to overcome this issue by carrying out the the following changes which worked on Sun System Java Application Server PE 9.0 only:

          ( i ) Re-create a single titan ejb project to include all 3 session beans (TravelAgent, TransactionPersistenceContext, ExtendedPersistenceContext) & 1 entity class (Cabin) in one titan.jar file. More importantly, have all classes pointing to 1 persistence unit (titan).

          The persistence.xml file was the root to my problem.

          ( ii ) Re-create Client with these syntax:

          ref = jndiContext.lookup("travelagent.TransactionPersistenceContextRemote");
          ref = jndiContext.lookup("travelagent.ExtendedPersistenceContextRemote");

          As a result, the Client output from Netbeans is:

          no cabin should be null: null
          Master Suite
          1
          1
          3
          Updating detached cabin instance with new bed count of 4
          Finding cabin to see it has been updated with a merge() on server
          new bed count is: 4
          Set up transaction persistence context stateful bean
          Cabin bed count will still be 4: 4
          Set up extended persistence context stateful bean
          Cabin bed count will be 5: 5

          However, I still could not find a solution when running in JBoss AS 4.0.5.

          Thanks,

          Henry

          • 2. Re: javax.naming.NamingException: Could not dereference obje
            htran_888

            Hi All,

            Could someone please help me with this issue?

            I still could not identify what the problem for so many weeks.

            Thanks,

            Henry

            • 3. Re: javax.naming.NamingException: Could not dereference obje
              kensloan

              I'm having a similar problem with ex05_1. Client_1 runs fine, but Client_2 gives the same error when it tries to bind to TransactionPersistenceContextBean/remote. The first bind to TravelAgentBean/remote works with no problem.

               public static void main(String [] args)
               {
               try
               {
               Context jndiContext = getInitialContext();
               Object ref = jndiContext.lookup("TravelAgentBean/remote");
               TravelAgentRemote dao = (TravelAgentRemote)ref;
              
              
               ref = jndiContext.lookup("TransactionPersistenceContextBean/remote");
               TransactionPersistenceContextRemote txBean = (TransactionPersistenceContextRemote)ref;
              
               Cabin fetchedCabin = dao.findCabin(1);
               int oldBedCount = fetchedCabin.getBedCount();
              

              I want to stress here that I am running the code "out of the box". Straight from the download with no modifications. No problems with all the previous exercises.

              Any help would be greatly appreciated.

              • 4. Re: javax.naming.NamingException: Could not dereference obje
                kensloan

                Here's some additional information. I don't understand it...perhaps someone here will.

                I went into the TransactionPersistenceContextBean and changed it from Stateful to Stateless, and it bound the TransactionPersistenceContextBean fine, but gave me the error on ExtendedPersistenceContextBean.

                So, I changed ExtendedPersistenceContextBean to Stateless. This time, it crashed with
                javax.naming.NameNotFoundException: ExtendedPersistenceContextBean not found

                So, just for grins, I changed the ExtendedPersistenceContextBean from
                type=PersistenceContextType.EXTENDED
                to
                type=PersistenceContextType.TRANSACTION
                and it ran all the way through with no problems. Of course, it's not what was intended, but what was intended DOESN'T FREAKING WORK!!! (that's right....all caps means shouting in forum-land).

                I think O'Reilly writes a great book and JBOSS is a great product, but by the 5th edition, you'd think they'd at least have the bugs out of the example code.

                Now, if it turns out I've somehow caused the problem, I will happily (albeit embarrassedly) retract this. But, as mentioned above, I started with it straight from download and ran all previous examples with no problems.

                Can anyone please shed some light on this?

                • 5. Re: javax.naming.NamingException: Could not dereference obje
                  tjclifford

                  I had this trouble too, and I found that I had to create a jboss.xml
                  under resources\META-INF for each example in the book (I'm up to
                  ch07 currently).

                  The one for ch05_1 is:

                  <jboss>
                   <enterprise-beans>
                   <session>
                   <ejb-name>TravelAgentBean</ejb-name>
                   <jndi-name>TravelAgentRemote</jndi-name>
                   </session>
                   <session>
                   <ejb-name>TransactionPersistenceContextBean</ejb-name>
                   <jndi-name>TransactionPersistenceContextRemote</jndi-name>
                   </session>
                   <session>
                   <ejb-name>ExtendedPersistenceContextBean</ejb-name>
                   <jndi-name>ExtendedPersistenceContextRemote</jndi-name>
                   </session>
                   </enterprise-beans>
                   <resource-ref>
                   <res-ref-name>jdbc/MySqlWorkbookDS</res-ref-name>
                   <resource-name>MySqlWorkbookDS</resource-name>
                   </resource-ref>
                  </jboss>
                  


                  ...I built this into the 'titan-ejb.jar' that I created just to hold the
                  class files from 'domain' and 'travelagent', but not 'clients'.
                  I created a separate 'titan-client.jar' with the client class files.

                  The datasource for mysql, in <resource-ref> above, I had to set up with
                  a 'mysql-ds.xml' file in the jboss deploy dir.

                  This took care of the errors I would get. In the clients, of course, you
                  also have to use:

                   Object ref = jndiContext.lookup("TravelAgentRemote");
                  


                  instead of:

                   Object ref = jndiContext.lookup("TravelAgentBean/remote");
                  


                  when using the above jboss.xml.
                  I could not find another way to get JNDI to see the remote interface.

                  Tom.


                  • 6. Re: javax.naming.NamingException: Could not dereference obje
                    satan3

                    Study