1 2 Previous Next 20 Replies Latest reply on Jan 9, 2009 4:47 AM by ezanih Go to original post
      • 15. Re: Could not find datasource message in client JPA although
        jaikiran

        ezanih,

        I wasn't aware you were running an application-client. I thought you were running a standalone java client. What exactly are you trying to do through the client - that will give us an idea whether you can just use a standalone client instead of the application client (which is a bit complicated and needs additional configurations to run under an application client container).

        • 16. Re: Could not find datasource message in client JPA although
          ezanih

          Sorry, Jaziran..it's my fault..I should have explained more clearly.

          Ok here's the big picture:-

          For now everything (server and client and db) is run on one PC - which is my PC running WinXP. The production version will have clients and server on separate PCs with a remote interface but that's later. I am using JDK5.14, Eclipse Ganymede and JBoss GA 4.2.2 and OracleXE.

          I want to create a JPA entity app first, get it working and then test it with a Java Application client. The whole app is much larger (a bidding auction involving JBoss ESB, jBPM and JRules among others),but for starters, I am just interested in getting the main engine working. So I start with the JPA entities routine and I just want to get to the point where the entities can be inserted/added, modified, deleted and searched (now through a Java Application client but later through a stateless EJB3 simply by adding the @Stateless annotation once the Java App client works.

          My server-side or JPA Entity project is called BiddingTest and consists only of Bidder.java, a simple bidder entity class with Bidder ID and Bidder name as the fields (as listed above). I used Enterprise Application Project wizard from Eclipse to create this server-side and linked it to an EAR5 project called BiddingTestEAR.

          Initially I did my client app as another Java class within the server-side but it did not make sense so I created a separate Application Client project in Eclipse and referenced it to the above server-side project. I referenced this client side too, to the same EAR5 project,BiddingTestEAR as above. The name of the client project is called BiddingClient and there is only one class with main() in it, its name is BidClient.java.

          In BiddingTest module/project, I have persistence.xml with a unit name of "BiddingTest". In my oracle-ds.xml file under server\default\deploy, I have :

          <jta-data-source>java:/OracleXE1_DS</jta-data-source>
          


          so my javax.sqlDataSource's JNDI name is OracleXE1_DS.

          The JBoss container has its own unique customization so you have to add jboss-client.xml and application-client.xml to the client project's META-INF directory. I also have my jndi.properties file here but I duplicated the properties code in BidClient.java.

          In the EAR module, BiddingTestEAR, under its META-INF folder I have my application.xml file.

          I linked BiddingTestEAR to the JBoss 4.2.2 Server in Eclipse and when I started the server, the server-side BiddingTest (comprising of the entity Bidder.java class) deploys OK and I can see the BIDDER table schema created in my OracleXE db. However of course, its contents are blank (it has no records).

          So I then fired up my client application by running it as a Java Application and it has given me a string of errors since my first posting on this forum. The adventure has taken me all the way to understanding that :-

          (a) You need to get the right combination of Hibernate components from the Hibernate Compability Matrix

          (b) You need to add jbossclient-all.jar and a string of specific jars to the classpath of the client (no beginner would no this the first time !!!)

          (c) You need to have application-client.xml and jboss-client.xml in the client's META-INF

          So although my server side entity creating table program (BiddingTest) is working, I am unable to insert records (for starters!) from a client program (BiddingClient). My jta datasource is called OracleXE1_DS.

          Up to this point in time, I am confused as to this portion of the code :-

          InitialContext cx = new InitialContext();
          


          I know this initializes the java naming in JBoss.

          But then I don't know what to "lookup" after the code above. Do I look up the JNDI-bound datasource name as such (which I defined in my persistence.xml and my oracle-ds.xml) :-

          DataSource obj = (DataSource) ctx.lookup("java:OracleXE1_DS");
          


          or do I lookup my Java App client project name (nothing to do with datasource) as such :-

          Object obj = ctx.lookup("java:BiddingClient");
          


          I know that my entity class is called Bidder.java. So instead of the above 2 code snippets, do i do this instead :-

          Bidder bidder = (Bidder) ctx.lookup(Bidder.class.getName());
          



          Up to this point, I have gotten rid of that annoying "Unable to find datasource" error message and I am only getting "BiddingClient not bound" and "OracleXE1_DS not bound" so it is just a case of getting the JNDI naming right for the client or probably the datasource.

          All the code to this project are as I have posted from the beginning should you wish to refer.

          This should be an easy project as it is just simple JPA entity creation but I guess running it as a Java EE on JBoss adds a layer of complexity!

          Please help! Where am I going wrong ?

          • 17. Re: Could not find datasource message in client JPA although
            ezanih

            Ok Jaikiran...by coincidence, you wrote a blog about JNDI naming in MyWiki in October 2007 which helped me understand (a bit) about my problem.

            Since I was not using an EJB, I was accessing the server directly through JNDI from my standalone client with the java:/OracleXE1_DS. As your blog said, I cannot do this with the java namespace because my standalone client and the JBoss server are running in separate JVMs.

            So I've decided to try 2 alternatives :-

            (1) To do a stateless EJB3 to access the datasource through the java: namespace. Since the EJB and the datasource are both managed by the JBoss Server, they should be both running in the same JBoss JVM.

            Here is my new context iniation code in the client. Instead of looking up the datasource through java namespace, it accesses the EJB3 :-

            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/remote");
             bidder.saveBidder();
            


            However, I'm getting a class cast exception as below. Is it because I am using a remote interface when I am running the database, JBoss, the EJB and the client all on only my computer - so I cannot use remote interface?

            - Container jboss.j2ee:ear=BiddingTestEAR.ear,jar=BiddingTestEJB.jar,name=BiddingTestBean,service=EJB3,VMID=413b70d7e6d9776b:-49ef6645:11eaf86b9cb:-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:87)
            Exception in thread "main" java.lang.NullPointerException
             at BidClient.main(BidClient.java:99)
            



            (2) One other way is to try to move the datasource out of the java namespace to the Global JNDI namespace in JBoss JNDI view. Do you know a way I can do this?

            • 18. Re: Could not find datasource message in client JPA although
              jaikiran

               

              "ezanih" wrote:


              javax.ejb.EJBException: Invalid (i.e. remote) invocation of local interface (null container)
              



              Looks like, the jndi-lookup returned a local interface instead of the remote interface of the bean. And as the message says, you are not allowed to do a remote method call on a local interface of the bean.
              Can you post the output of JNDI tree and also the code of the bean and the BiddingTestBeanRemote interface (including the annotations and import statements).



              "ezanih" wrote:

              (2) One other way is to try to move the datasource out of the java namespace to the Global JNDI namespace in JBoss JNDI view. Do you know a way I can do this?


              Answered in your other post in JNDI forum http://www.jboss.com/index.html?module=bb&op=viewtopic&t=148230 :)

              • 19. Re: Could not find datasource message in client JPA although
                ezanih

                Thank you so much, Jaikiran :-)

                So am I right in my conclusion that if I call this portion of code directly from my standalone Java 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");
                
                java.sql.Connection conn;
                InitialContext ic = new InitialContext(p);
                javax.sql.DataSource db = (javax.sql.DataSource) ic.lookup("java:OracleXE1_DS");
                conn = db.getConnection();
                


                to lookup a wrapper datasource from a JPA entity project saved in my JBoss AS using EntityManager, it would always give me a "XXX not bound" exception because the JBoss AS and the Java client are running on separate VMs ?

                Would the solution be : to create an EJB3 to get the Java client to lookup the EJB3 instead - ctx.lookup("bean/local") - and then have the bean call the wrapper datasource - ctx.lookup("java:/DefaultDS") - assuming it would work because the EJB and the wrapper datasource are both managed by the JBoss AS container and so the java:/ namespace can be used because they are using the same VM (JBoss VM) ?

                • 20. Re: Could not find datasource message in client JPA although
                  ezanih

                  Phew...sigh! After 23 days, working on my JPA entity program, I finally got it working with the help of a EJB3.0 layer in between my calling client/servlet/jsp and my JPA entities and the database.

                  A few things I learnt (for the benefit of others) :-

                  (1) Be careful to get compatible Hibernate components by looking at the Compatibility Matrix

                  (2) Whatever is in the JBoss client folder will override whatever you add to the classpath in Eclipse (so you should replace the files directly in the JBoss client folder)

                  (3) Be sure to get the JNDI naming absolutely spot-on (type exactly as the JNDI View shows in JMX-console in JBoss) - read Jaikiran's Oct 2007 article

                  (4) Learn the difference between java namespacing and global JNDI namespacing and that a standalone client cannot directly access a java:/ namespace (eg. a wrapper datasource) if the JPA entity is being managed by the JBoss server container which is on a different JVM

                  (5) learn the additional configuration work that you need to do on a JBoss server (such as setting the JNDI properties)

                  (6) learn exactly what the server does in terms of transaction mgmt when you use JTA and Hibernate EntityManager. A lot of code that you type may not be needed (especially if you use annotation and injection)

                  So that's it! Thank you all! Thank you Jaikiran! I'm off to expand my program further by adding additional RichFaces, Seam, JBoss ESB, jBPM and Rules components so I'll be returning to the forum in no time !!! :-)

                  1 2 Previous Next