12 Replies Latest reply on Jan 9, 2009 4:35 AM by ezanih

    $Proxy200 error & Invalid (i.e. remote) invocation of local

    ezanih

      Hi there

      I have a Java client and a HTTP servlet accessing a stateless EJB3 which accesses a wrapper datasource to persist an entity to my OracleXE database.

      I am using JBoss 4.2.2.GA and Eclipse Ganymede and JDK 5.0_14.

      When I tried to access the wrapper datasource directly from a standalone Java client through this initial context code,

      Access wrapper ds from client:

      Properties p = new Properties();
      p.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
      p.put("java.naming.provider.url", "jnp://localhost:1099");
      p.put("java.naming.factory.url.pkgs", "org.jboss.naming.client");
      
      InitialContext ic = new InitialContext(p);
      javax.sql.DataSource db = (javax.sql.DataSource) ic.lookup("java:OracleXE1_DS");
      


      I get a naming context 'XXX not bound' exception. I finally found out that this was because my Java client is in a separate JVM from the wrapper datasource running inside my JBoss server's JVM and so, I will not be able to use the java namespace (as above).

      So I modified my Eclipse project(s) to use a stateless EJB3, the logic being, that the EJB and the wrapper datasource will be managed inside the JBoss container and so share the same container's JVM.

      In my EJB3, I have both a remote and a local interface. No problems deploying the bean and I checked my JMX console and found the following entries :-

      JMX JNDI View:
      +- BiddingTestEAR (class: org.jnp.interfaces.NamingContext)
       | +- BiddingTestBeanLocal (class: org.jnp.interfaces.NamingContext)
       | | +- local (proxy: $Proxy105 implements interface my.com.eperolehan.ejb.BiddingTestBeanLocalLocal,interface org.jboss.ejb3.JBossProxy)
       | +- BiddingTestBean (class: org.jnp.interfaces.NamingContext)
       | | +- local (proxy: $Proxy101 implements interface my.com.eperolehan.ejb.BiddingTestBeanRemote,interface org.jboss.ejb3.JBossProxy)
      


      However when I try to do a context initiation first using the remote interface then using the local interface, both of them failed. I tried accessing from a standalone Java client and from a default.jsp page.

      LOCAL BEAN INTERFACE

      Context initiation code snippet:
      BiddingTestBeanLocal bidder = (BiddingTestBeanLocal) ctx.lookup("BiddingTestEAR/BiddingTestBeanLocal/local");
      


      Error console for the jsp:
      17:26:13,296 INFO [TomcatDeployer] deploy, ctxPath=/BiddingWeb, warUrl=.../tmp/deploy/tmp34742BiddingTestEAR.ear-contents/BiddingWeb-exp.war/
      17:26:13,421 INFO [EARDeployer] Started J2EE application: file:/C:/Program Files/jboss-4.2.2.GA/server/default/deploy/BiddingTestEAR.ear
      17:26:22,875 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
      java.lang.ClassCastException: $Proxy200
      



      Error console for the standalone client:
      java.lang.ClassCastException: $Proxy0
       at BidClient.main(BidClient.java:83)
      Exception in thread "main" java.lang.NullPointerException
       at BidClient.main(BidClient.java:100)
      



      REMOTE BEAN INTERFACE

      Context initiation code snippet:
      Properties p = new Properties();
      p.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
      p.put("java.naming.provider.url", "jnp://localhost:1099");
      p.put("java.naming.factory.url.pkgs", "org.jboss.naming.client");
      
      InitialContext ctx = new InitialContext(p);
      BiddingTestBeanRemote bidder= (BiddingTestBeanRemote) ctx.lookup("BiddingTestEAR/BiddingTestBean/local");
      


      Error console for the default.jsp:
      javax.ejb.EJBException: java.lang.IllegalStateException: Illegal to call this method from injected, managed EntityManager
      


      Error console for the standalone client:
      - Container jboss.j2ee:ear=BiddingTestEAR.ear,jar=BiddingTestEJB.jar,name=BiddingTestBean,service=EJB3,VMID=413b70d7e6d9776b:377efe3:11eb05d9788:-7fff is not yet available
      javax.ejb.EJBException: Invalid (i.e. remote) invocation of local interface (null container)
       at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:80)
       at $Proxy0.saveBidder(Unknown Source)
       at BidClient.main(BidClient.java:88)
      Exception in thread "main" java.lang.NullPointerException
       at BidClient.main(BidClient.java:100)
      
      



      and yes, I am using PersistenceContext to inject the persistence unit into the EntityManager like so:

      @PersistenceContext(unitName="BiddingTest")
      private static EntityManager em;
      



      The declaration for my EJB3 implementation is:

      @Stateless
      public class BiddingTestBean implements BiddingTestBeanRemote {
      


      and I have also changed it to

      @Stateless
      public class BiddingTestBean implements BiddingTestBeanLocal {
      



      What seems to be the problem?


        • 1. Re: $Proxy200 error & Invalid (i.e. remote) invocation of lo
          jaikiran

          Use the @Remote annotation to mark the interface as remote:

          @Stateless
          @Remote (BiddingTestBeanRemote.class)
          public class BiddingTestBean implements BiddingTestBeanRemote


          • 2. Re: $Proxy200 error & Invalid (i.e. remote) invocation of lo
            ezanih

            Ok, you are right :-) ...JNDI now returns a remote namespace:

            JMX Console's JNDI View:

             +- BiddingTestEAR (class: org.jnp.interfaces.NamingContext)
             | +- BiddingTestBeanLocal (class: org.jnp.interfaces.NamingContext)
             | | +- local (proxy: $Proxy107 implements interface my.com.eperolehan.ejb.BiddingTestBeanLocalLocal,interface org.jboss.ejb3.JBossProxy)
             | +- BiddingTestBean (class: org.jnp.interfaces.NamingContext)
             | | +- remote (proxy: $Proxy102 implements interface my.com.eperolehan.ejb.BiddingTestBeanRemote,interface org.jboss.ejb3.JBossProxy)
            



            However, I am now getting this rather simple 2-liner error :

            java.lang.NullPointerException
            at BidClient.main(BidClient.java:95)
            


            and the code pointed to is this :

            em.getTransaction().begin();
            


            I've moved all the em.getTransaction().begin(), em.persist(bidder) and em.getTransaction().commit() calls out of the EJB3 and into the client because originally putting them in the EJB3 was giving me this error :

            Console:
            10:43:46,984 ERROR [STDERR] java.lang.IllegalStateException: Illegal to call this method from injected, managed EntityManager
            


            It seems that whenever I use the code below, I get the above error:

            @PersistenceContext(unitName="BiddingTest")
            private static EntityManager em;
            


            So I changed from injection to this :

            private static EntityManagerFactory emf;
            private static EntityManager em;
            
            emf = Persistence.createEntityManagerFactory("BiddingTest");
            em = emf.createEntityManager();
            


            Then I got this error which I believe, from the forums, is due to incompatibilities from the Hibernation Compatibility Matrix :

            Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.cfg.AnnotationConfiguration.setProperty(Ljava/lang/String;Ljava/lang/String;)Lorg/hibernate/cfg/AnnotationConfiguration;
            


            However, I made sure I was compatible by using the latest Hibernate downloads and including the jars as well as the supporting and required jars found in the lib of that Hibernate product.

            Here is what I've added to the classpath:

            antlr-2.7.6.jar
            commons-collections-3.1.jar
            commons-logging-1.1.1.jar
            concurrent.jar
            dom4j-1.6.1.jar
            ejb3-persistence.jar
            hibernate-annotations.jar
            hibernate-commons-annotations.jar
            hibernate-core.jar
            hibernate-entitymanager.jar
            javassist-3.4.GA.jar
            jboss-common-client.jar
            jboss-j2ee.jar
            jboss-transaction-client.jar
            jbossall-client.jar
            jbosssx-client.jar
            jnp-client.jar
            jnpserver.jar
            jta-1.1.jar
            log4j.jar
            slf4j-api-1.5.2.jar
            slf4j-jdk14-1.5.2.jar


            My Hibernate versions:

            - Hibernate Distribution 3.3.1.GA
            - Hibernate EntityManager 3.4.0.GA
            - Hibernate Annotations 3.4.0.GA


            Ugh...still no progress, but I would like to ask you a few questions also:-

            (1) Should the EntityManagerFactory be created only once (since it is a factory) and where is the best place to put its creation? Should we make the creation a singleton and how?

            (2) Should the EntityManager be created in the EJB or in the java client or servlet or jsp ?

            (3) Where do I place all the "em.getTransaction().begin(), em.persist(bidder) and em.getTransaction().commit()" commands? In the client/servlet/jsp or in the EJB?

            Thank you so much in advance.

            • 3. Re: $Proxy200 error & Invalid (i.e. remote) invocation of lo
              itsme

              If you do all that persisting stuff into a (or that) EJB3 your problem should be solved. Especially dealing with transactions is not needed for most scenarios while the EJB3 (or better the EJB3-Container) is doing that for you by default.

              Hope this helps.

              • 4. Re: $Proxy200 error & Invalid (i.e. remote) invocation of lo
                ezanih

                Hi itsme

                You are right...it is all managed by the container especially when using entity managers but surely you have to be calling em.gettransaction.begin(), em.gettransaction.commit(),em.persist(object) from somewhere in code to make it happen !

                • 5. Re: $Proxy200 error & Invalid (i.e. remote) invocation of lo
                  ezanih

                  Hmmm...I still seem to be bouncing between ReflectionManager class not found exceptions and AnnotationConfiguration class not found exceptions ...despite checking that I've got the right combination of Hibernate components from the Compatibility Matrix (see my previous post). Could it be a possible addition of any other jars into my classpath ?

                  • 6. Re: $Proxy200 error & Invalid (i.e. remote) invocation of lo
                    ezanih

                    The error message on my Eclipse console stubbornly shows that I am still using Hibernate Annotations 2.1.GA despite me adding the latest versions of HA to the classpath. I have also tried adding them to the lib folder of server\default. That I guess is the source of my error but how do I get Eclipse to recognise that I'm using a later version of HA than 2.1.GA ???

                    Eclipse Console error message:

                    - Hibernate Annotations 3.2.1.GA
                    Jan 8, 2009 3:08:51 PM org.hibernate.cfg.Environment <clinit>
                    INFO: Hibernate 3.3.0.SP1
                    Jan 8, 2009 3:08:51 PM org.hibernate.cfg.Environment <clinit>
                    INFO: hibernate.properties not found
                    Jan 8, 2009 3:08:51 PM org.hibernate.cfg.Environment buildBytecodeProvider
                    INFO: Bytecode provider name : javassist
                    Jan 8, 2009 3:08:51 PM org.hibernate.cfg.Environment <clinit>
                    INFO: using JDK 1.4 java.sql.Timestamp handling
                    Jan 8, 2009 3:08:51 PM org.hibernate.ejb.Version <clinit>
                    INFO: Hibernate EntityManager 3.4.0.GA
                    Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.cfg.AnnotationConfiguration.setProperty(Ljava/lang/String;Ljava/lang/String;)Lorg/hibernate/cfg/AnnotationConfiguration;
                     at org.hibernate.ejb.Ejb3Configuration.setProperty(Ejb3Configuration.java:535)
                     at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:165)
                     at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:253)
                     at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:125)
                     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
                     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
                     at BidClient.main(BidClient.java:53)
                    


                    • 7. Re: $Proxy200 error & Invalid (i.e. remote) invocation of lo
                      ezanih

                      I've just narrowed my problem down to this error (see console output below) where my client app always shows that I am using Hibernate Annotations 3.2.1.GA no matter what HA version I'm adding to the client classpath in Eclipse or directly to the JBoss's server/default/lib. It's pretty strange and stubborn too. This first line below always comes out in black and the rest in red in the console output:

                      Client Side Console Output:

                      - Hibernate Annotations 3.2.1.GA
                      Jan 8, 2009 5:11:11 PM org.hibernate.cfg.Environment <clinit>
                      INFO: Hibernate 3.3.0.SP1
                      Jan 8, 2009 5:11:11 PM org.hibernate.cfg.Environment <clinit>
                      INFO: hibernate.properties not found
                      Jan 8, 2009 5:11:11 PM org.hibernate.cfg.Environment buildBytecodeProvider
                      INFO: Bytecode provider name : javassist
                      Jan 8, 2009 5:11:11 PM org.hibernate.cfg.Environment <clinit>
                      INFO: using JDK 1.4 java.sql.Timestamp handling
                      Jan 8, 2009 5:11:12 PM org.hibernate.ejb.Version <clinit>
                      INFO: Hibernate EntityManager 3.4.0.GA
                      Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.cfg.AnnotationConfiguration.setProperty(Ljava/lang/String;Ljava/lang/String;)Lorg/hibernate/cfg/AnnotationConfiguration;
                       at org.hibernate.ejb.Ejb3Configuration.setProperty(Ejb3Configuration.java:535)
                       at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:165)
                       at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:253)
                       at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:125)
                       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
                       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
                       at BidClient.main(BidClient.java:54)
                      
                      


                      Strangely enough, for the server-side output (when I start JBoss from Eclipse), it seems to correctly show that I am using Hibernate Annotations 3.4.0.GA (see below):

                      Server-Side Console output:
                      17:34:39,562 INFO [Version] Hibernate Annotations 3.4.0.GA
                      17:34:39,578 INFO [Environment] Hibernate 3.3.0.SP1
                      17:34:39,593 INFO [Environment] hibernate.properties not found
                      17:34:39,593 INFO [Environment] Bytecode provider name : javassist
                      17:34:39,593 INFO [Environment] using JDK 1.4 java.sql.Timestamp handling
                      17:34:39,718 INFO [Version] Hibernate Commons Annotations 3.1.0.GA
                      17:34:39,718 INFO [Version] Hibernate EntityManager 3.4.0.GA
                      17:34:39,968 INFO [AnnotationBinder] Binding entity from annotated class: my.com.eperolehan.entities.Bidder
                      17:34:40,015 INFO [EntityBinder] Bind entity my.com.eperolehan.entities.Bidder on table BIDDER
                      17:34:40,109 INFO [Version] Hibernate Validator 3.0.0.GA
                      


                      What could be the problem ?

                      • 8. Re: $Proxy200 error & Invalid (i.e. remote) invocation of lo
                        ezanih

                        Ok, I've finally solved the problem. Since I'm using JBoss server, it seems that everything that I put directly on the classpath is overriden by what's in the JBoss client directory especially the hibernate files. So anybody using JBoss, do be careful especially when you are doing the Hibernate Compatibility Matrix portion.

                        Now all I need to know is, since my client is calling my session bean is calling my wrapper datasource which persists the entity to db, where do I place my em.gettransaction.begin(), em.persist(entity), em.gettransaction.commit() and em.gettransaction.rollback() commands?

                        Do I do it in the client as follows :-

                         BiddingTestBeanRemote bidder= (BiddingTestBeanRemote) ctx.lookup("BiddingTestEAR/BiddingTestBean/remote");
                         //BiddingTestBeanLocal bidder = (BiddingTestBeanLocal) PortableRemoteObject.narrow (obj, BiddingTestBean.class);
                        
                        em.getTransaction().begin();
                        bidder.setBidder(); // call the bean's setter method
                        em.persist(bidder);
                        em.getTransaction().commit();
                        


                        or in the bean as follows :-

                        em.getTransaction().begin();
                        Bidder bidder = new Bidder();
                        bidder.setBidderName("Bidder No 1");
                        bidder.setBidSessionId(1);
                        em.persist(bidder);
                        em.getTransaction().commit();
                        



                        Please can somebody help.

                        • 9. Re: $Proxy200 error & Invalid (i.e. remote) invocation of lo
                          wolfgangknauf

                          Hi,

                          to comment on one of your older questions:

                          You are right...it is all managed by the container especially when using entity managers but surely you have to be calling em.gettransaction.begin(), em.gettransaction.commit(),em.persist(object) from somewhere in code to make it happen !


                          By default a transaction is started automatically when you call a session bean method from the client, and it is committed afterwards. So you don't need to care about them.
                          More control over transaction (e.g. you want to start new one if one session bean calls another bean's method) can be achieved through annotations like @RequiresNew.

                          And only if all this is not sufficient, you can perform your own transaction handling. But in most use cases, the default behavior is good enough I think.

                          Hope this helps

                          Wolfgang

                          • 10. Re: $Proxy200 error & Invalid (i.e. remote) invocation of lo
                            ezanih

                            Thank you so much Wolfgang..this I did not know :-)

                            So does this mean I do not need to call em.getTransaction.begin(), em.getTransaction.commit() and em.getTransaction.rollback() but it still means I need to call em.persist(myEntity) or em.delete(myEntity) from inside the bean, right ?

                            (This is linked to my later question on where is the most suitable place to instantiate the EntityManagerFactory and EntityManager.)

                            • 11. Re: $Proxy200 error & Invalid (i.e. remote) invocation of lo
                              itsme

                               

                              So does this mean I do not need to call em.getTransaction.begin(), em.getTransaction.commit() and em.getTransaction.rollback() but it still means I need to call em.persist(myEntity) or em.delete(myEntity) from inside the bean, right ?


                              Yes. It does.

                              • 12. Re: $Proxy200 error & Invalid (i.e. remote) invocation of lo
                                ezanih

                                Itsme

                                You are absolutely, correctly right! With JBoss AS, and injecting the persistence unit into the EntityManager, you

                                (1) don't even have to create an EntityManagerFactory. It's all done by the container. You just inject the EntityManager with the @PersistenceContext annotation and all this should be done in the EJB3 not the client or calling servlet or calling jsp page.

                                (2) don't have to call em.getTransaction.begin(), em.getTransaction.commit() or em.getTransaction.rollback(). This is all handled auto by the container when the EJB3 is called.

                                This has certainly made programming much easier with less verbose code!

                                :-)