1 2 Previous Next 20 Replies Latest reply on Jan 9, 2009 4:47 AM by ezani abdul halim

    Could not find datasource message in client JPA although EAR

    ezani abdul halim Newbie

      Hi there

      I created a very simple JPA Entity Project (BiddingTest) involving one bid entity (Bidder.java) and one bid client (BidClient.java) from the JPA Project Wizard in Eclipse Ganymede and JBoss 4.2.2.GA. I use Hibernate 3.3.1, Hibernate Annotations 3.4 and Hibernate Entity Manager 3.4.

      My BiddingTestEAR was built and deployed succesfully by the JBoss Server and I checked in my OracleXE db and found out that it had successfully managed to create the BIDDER table.

      However my problem occurs when I try to populate this BIDDER table from a JPA Test Client. The JPA Test Client keeps giving me the exception :

      javax.persistence.PersistenceException: [PersistenceUnit: BiddingTest] Unable to build EntityManagerFactory


      and

      Caused by: org.hibernate.HibernateException: Could not find datasource


      and

      Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
      .


      Here is my console output:

      [BidClient] : creating EntityManagerFactory...
      log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
      log4j:WARN Please initialize the log4j system properly.
      javax.persistence.PersistenceException: [PersistenceUnit: BiddingTest] Unable to build EntityManagerFactory
       at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
       at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
       at Main.main(Main.java:43)
      Caused by: org.hibernate.HibernateException: Could not find datasource
       at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:79)
       at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:137)
       at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
       at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:89)
       at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2101)
       at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1325)
       at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
       at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
       ... 4 more
      Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
       at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
       at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
       at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
       at javax.naming.InitialContext.lookup(InitialContext.java:351)
       at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)
       ... 11 more
      [BidClient] : closing entity manager and factory...
      Exception in thread "main" java.lang.NullPointerException
       at Main.main(Main.java:88)
      



      This is my persistence.xml file :

      <?xml version="1.0" encoding="UTF-8"?>
      <persistence>
       <persistence-unit name="BiddingTest">
       <jta-data-source>java:/OracleXE1_DS</jta-data-source>
       <properties>
       <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
       <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
       </properties>
       </persistence-unit>
      </persistence>
      



      This is my oracle-ds.xml file :

      <?xml version="1.0" encoding="UTF-8"?>
      <datasources>
       <local-tx-datasource>
       <jndi-name>OracleXE1_DS</jndi-name>
       <connection-url>jdbc:oracle:thin:system/system@localhost:1521:XE</connection-url>
       <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
       <user-name>system</user-name>
       <password>system</password>
       <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
       <metadata>
       <type-mapping>Oracle10g</type-mapping>
       </metadata>
       </local-tx-datasource>
      </datasources>
      


      This is my BidClient.java file:

      
      package my.com.eperolehan.client;
      
      
      import javax.persistence.EntityManager;
      //import javax.persistence.EntityTransaction;
      import javax.persistence.EntityManagerFactory;
      import javax.persistence.Persistence;
      //import javax.persistence.PersistenceContext;
      
      import my.com.eperolehan.entities.Bidder;
      
      import org.apache.log4j.Level;
      import org.apache.log4j.Logger;
      import org.apache.log4j.ConsoleAppender;
      import org.apache.log4j.SimpleLayout;
      
      
      public class BidClient {
      
      
       private static EntityManagerFactory f;
      
       //@PersistenceContext(unitName="BiddingTest")
       private static EntityManager em;
      
      
       public static void main(String[] args) {
      
      
       Logger log = Logger.getLogger(BidClient.class);
       log.setLevel(Level.WARN);
      
       SimpleLayout layout = new SimpleLayout();
       ConsoleAppender appender = new ConsoleAppender(layout);
      
      
       log.addAppender(appender);
      
      
       try {
      
      
       System.out.println("[BidClient] : creating EntityManagerFactory...");
       log.debug("[BidClient] : creating EntityManagerFactory...");
       f = Persistence.createEntityManagerFactory("BiddingTest");
       System.out.println("[BidClient] : Created.");
       log.debug("[BidClient] : Created.");
       System.out.println("[BidClient] : creating EntityManager...");
       em = f.createEntityManager();
       System.out.println("[BidClient] : Created.");
       log.debug("[BidClient] : Created.");
      
      
       em.getTransaction().begin();
      
      
       try {
       System.out.println("[BidClient] : creating a Bidder...");
       Bidder bidder = new Bidder();
      
       bidder.setBidderName("Bidder No 1");
       bidder.setBidSessionId(1);
      
      
       em.persist(bidder);
       em.getTransaction().commit();
       System.out.println("[BidClient] : Bidder created and commited.");
       log.debug("[BidClient] : Bidder created and commited.");
      
       } catch (Exception ex) {
       em.getTransaction().rollback();
       }
      
       System.out.println("[BidClient] : Bidder person created and persisted successfully.");
       log.debug("[BidClient] : Bidder person created and persisted successfully.");
      
       } catch (RuntimeException e) {
      
       e.printStackTrace();
      
       } finally {
      
      
      
       }
      
      
       System.out.println("[BidClient] : closing entity manager and factory...");
       log.debug("[BidClient] : closing entity manager and factory...");
       em.close();
       //f.close();
       System.out.println("[BidClient] : Closed.");
       log.debug("[BidClient] : Closed.");
      
       }
      
      
      }
      


      Please can help me. Thank you very much in advance!

        • 1. Re: Could not find datasource message in client JPA although
          jaikiran pai Master

           

          javax.persistence.PersistenceException: [PersistenceUnit: BiddingTest] Unable to build EntityManager
          Factory
          at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
          at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)

          at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
          at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
          at Main.main(Main.java:43)
          Caused by: org.hibernate.HibernateException: Could not find datasource
          at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.jav
          a:79)
          at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFacto
          ry.java:137)
          at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.jav
          a:29)
          at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:89)
          at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2101)
          at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1325)
          at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
          at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
          ... 4 more
          Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or syst
          em property, or as an applet parameter, or in an application resource file: java.naming.factory.ini
          tial
          at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
          at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
          at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
          at javax.naming.InitialContext.lookup(InitialContext.java:351)
          at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.jav
          a:75)


          You need to place a jndi.properties in the client's classpath. See this for more details http://www.jboss.org/community/docs/DOC-9816

          • 2. Re: Could not find datasource message in client JPA although
            ezani abdul halim Newbie

            Hi jakiran

            There is already a jndi.properties file in my C:\Program Files\jboss-4.2.2.GA\server\default\conf folder with the normal 4 lines : DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING. I haven't touched this jndi.properties file at all so could you be more specific as to how to add this file to my client JPA classpath in Eclipse?

            • 3. Re: Could not find datasource message in client JPA although
              ezani abdul halim Newbie

              Hi there

              One point I missed out...

              Do I have to add all these additional JNDI locating code in my BidClient.class or do I just add the jndi.properties file to my classpath (and how do I do that) :


              Properties p = new Properties();
              
               p.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory ");
               p.put("java.naming.provider.url", "localhost:1099");
              
              
               InitialContext ctx = new InitialContext( p );
              
               Object obj = ctx.lookup("/BiddingTest");
              


              Many thanks in advance!

              • 4. Re: Could not find datasource message in client JPA although
                jaikiran pai Master

                 

                There is already a jndi.properties file in my C:\Program Files\jboss-4.2.2.GA\server\default\conf folder

                That one is on the server. You will have to place the jndi.properties in the client (BidClient.class) classpath as well. I am not exactly sure how this can be done through Eclipse.

                Another point:

                p.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory ");


                There should not be a trailing space in the value. It should be:

                p.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");



                • 5. Re: Could not find datasource message in client JPA although
                  ezani abdul halim Newbie

                  Hi Jaziran

                  Thank you for monitoring this blog...I appreciate it...I think I'm very close to getting the client working. Like I mentioned intially, my server side JPA entity is deploying OK ... but I'm trying to get my Java Application client to access it. Once that works I will access through JSP.

                  Ok...back to the issue at hand...I have put the jndi.properties in JBoss's server/default/deploy/conf directory where I added a new line jboss....provider.url = localhost.

                  And I have put the jndi.properties in jre6/ directory where I added a new line jboss....provider.url = localhost:1099. Probably this is wrong..if it doesn't work, I'm going to open the BiddingClient.jar manually with WinZip and put jndi.properties under /META-INF.

                  One important thing...if you look at my original BidClient.class client code...I didn't have any of those JNDI locating InitialContext code at all. Do I really need to add those additional codes with all the properties and InitialContext (at the beginning) in my BidClient.class? Is it necessary for EJB3 or just for EJB2.1 only? Shouldn't @PersistenceContext(unit="BiddingTest") suffice?

                  I am confused with all these lib folders. They are everywhere...in the JDK, in the JRE, in JBoss main folder, in JBoss server folders and in all 3rd party Java applications I installed such as Hibernate, Seam, etc. Yesterday I put a host of new jars in my jdk6/jre6/lib/ext folder and my JBoss refused to start until I removed them all back to the initial 6 files! Is there any place I can put all my application-unique external jars so that I know that Application A uses this set of jars, Application B uses this set of jars, etc rather than having them all mixed up in one folder?

                  Will keep you updated on the progress of my BidClient whether can connect or not...

                  • 6. Re: Could not find datasource message in client JPA although
                    ezani abdul halim Newbie

                    Hi

                    I've changed my BidClient.class by adding the InitialContext code early in the class (see below) but it is giving me this error (server side is deploying OK no errors and BIDDER table created) :

                    Console:

                    Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]
                     at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)
                     at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
                     at javax.naming.InitialContext.init(InitialContext.java:223)
                     at javax.naming.InitialContext.<init>(InitialContext.java:197)
                     at Main.main(Main.java:54)
                    Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory
                     at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
                     at java.security.AccessController.doPrivileged(Native Method)
                     at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
                     at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
                     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
                     at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
                     at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
                     at java.lang.Class.forName0(Native Method)
                     at java.lang.Class.forName(Class.java:242)
                     at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:42)
                     at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)
                     ... 4 more
                    



                    This is my revised BidClient.class (I run it as a Java Application in Eclipse) with the InitialContext code on top:

                    JPA Client Coding:
                    
                    
                    import javax.persistence.EntityManager;
                    import javax.persistence.EntityManagerFactory;
                    import javax.persistence.Persistence;
                    import javax.persistence.PersistenceContext;
                    
                    import org.apache.log4j.Level;
                    import org.apache.log4j.Logger;
                    import org.apache.log4j.ConsoleAppender;
                    import org.apache.log4j.SimpleLayout;
                    
                    import my.com.eperolehan.entities.Bidder;
                    
                    import javax.rmi.*;
                    import javax.naming.InitialContext;
                    import javax.naming.NamingException;
                    
                    import java.util.*;
                    
                    
                    
                    public class Main {
                    
                    
                     private static EntityManagerFactory f;
                    
                     @PersistenceContext(unitName="BiddingTest")
                     private static EntityManager em;
                    
                    
                     public static void main(String[] args) throws NamingException {
                    
                    
                     Logger log = Logger.getLogger(Main.class);
                     log.setLevel(Level.WARN);
                    
                     SimpleLayout layout = new SimpleLayout();
                     ConsoleAppender appender = new ConsoleAppender(layout);
                    
                    
                     log.addAppender(appender);
                    
                    
                     try {
                    
                    
                     Properties p = new Properties();
                    
                     p.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                     p.put("java.naming.provider.url", "localhost:1099");
                    
                    
                     InitialContext ctx = new InitialContext( p );
                    
                     Object obj = ctx.lookup("/Bidder");
                     Bidder bidder = (Bidder) PortableRemoteObject.narrow(obj,Bidder.class);
                    
                    
                     System.out.println("[BidClient] : creating EntityManagerFactory...");
                     log.debug("[BidClient] : creating EntityManagerFactory...");
                     f = Persistence.createEntityManagerFactory("BiddingTest");
                     System.out.println("[BidClient] : Created.");
                     log.debug("[BidClient] : Created.");
                     System.out.println("[BidClient] : creating EntityManager...");
                     em = f.createEntityManager();
                     System.out.println("[BidClient] : Created.");
                     log.debug("[BidClient] : Created.");
                    
                    
                     em.getTransaction().begin();
                    
                    
                     try {
                     System.out.println("[BidClient] : creating a Bidder...");
                     //Bidder bidder = new Bidder();
                    
                     bidder.setBidderName("Bidder No 1");
                     bidder.setBidSessionId(1);
                    
                    
                     em.persist(bidder);
                     em.getTransaction().commit();
                     System.out.println("[BidClient] : Bidder created and commited.");
                     log.debug("[BidClient] : Bidder created and commited.");
                    
                     } catch (Exception ex) {
                     em.getTransaction().rollback();
                     }
                    
                     System.out.println("[BidClient] : Bidder person created and persisted successfully.");
                     log.debug("[BidClient] : Bidder person created and persisted successfully.");
                    
                     } catch (RuntimeException e) {
                    
                     e.printStackTrace();
                    
                     } finally {
                    
                    
                    
                     }
                    
                    
                     System.out.println("[BidClient] : closing entity manager and factory...");
                     log.debug("[BidClient] : closing entity manager and factory...");
                     em.close();
                     //f.close();
                     System.out.println("[BidClient] : Closed.");
                     log.debug("[BidClient] : Closed.");
                    
                     }
                    
                    
                    }
                    



                    What's wrong?


                    Many thanks in advance!
                    Ezani

                    • 7. Re: Could not find datasource message in client JPA although
                      jaikiran pai Master

                       

                      Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory


                      This error indicates that your client classpath is missing the jbossall-client.jar file. Place it in the client classpath. You can copy it from %JBOSS_HOME%/client folder.

                      • 8. Re: Could not find datasource message in client JPA although
                        ezani abdul halim Newbie

                        Thanks for the reply, jakiran.

                        Ok...I've been doing a lot of reading so let me share with you and all some knowledge :-) but my client still not working :-(

                        There are 3 ways to add jndi.properties to the client app :- (1) from the Eclipse Run Configuration menu, just add this to the VM arguments:-

                        -Djava.naming.provider.url=jnp://localhost:1099
                        -Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
                        


                        or you can do like my BidClient.java coding above and put the properties in code

                        or you can just create a text file, name it jndi.properties and place it in root folder of client app. I've modified my app for this method so I can comment out all the InitialContext code additions and just put

                        InitialContext ctx = InitialContext();
                        


                        Much cleaner...Ok...that is settled....

                        Now I've read some more and it seems JBoss needs two more configurator files at the client side, namely application-client.xml and jboss-client.xml.

                        I've added these 2 files :-

                        application-client.xml:

                        <?xml version="1.0" encoding="UTF-8"?>
                        
                        <application-client xmlns="http://java.sun.com/xml/ns/j2ee"
                         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                         http://java.sun.com/xml/ns/j2ee/application-client_1_4.xsd"
                         version="1.4">
                        
                         <display-name>BiddingClient</display-name>
                        
                         <resource-ref>
                         <res-ref-name>java:OracleXE1_DS</res-ref-name>
                         <res-type>javax.sql.DataSource</res-type>
                         <res-auth>Container</res-auth>
                         </resource-ref>
                        
                        </application-client>
                        


                        jboss-client.xml:

                        <jboss-client>
                         <resource-ref>
                         <res-ref-name>java:OracleXE1_DS</res-ref-name>
                         <res-type>javax.sql.DataSource</res-type>
                         <jndi-name>OracleXE1_DS</jndi-name>
                         </resource-ref>
                        </jboss-client>
                        



                        Unfortunately, still getting the same error...Unable to find data source and the jndi initial naming error, see below:

                        Console output :

                        [BidClient] : creating EntityManagerFactory...
                        log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
                        log4j:WARN Please initialize the log4j system properly.
                        javax.persistence.PersistenceException: [PersistenceUnit: BiddingTest] Unable to build EntityManagerFactory
                         at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
                         at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
                         at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
                         at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
                         at Main.main(Main.java:61)
                        Caused by: org.hibernate.HibernateException: Could not find datasource
                         at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:79)
                         at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:137)
                         at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
                         at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:89)
                         at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2101)
                         at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1325)
                         at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
                         at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
                         ... 4 more
                        Caused by: javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]
                         at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)
                         at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
                         at javax.naming.InitialContext.init(InitialContext.java:223)
                         at javax.naming.InitialContext.<init>(InitialContext.java:175)
                         at org.hibernate.util.NamingHelper.getInitialContext(NamingHelper.java:51)
                         at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)
                         ... 11 more
                        Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory
                         at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
                         at java.security.AccessController.doPrivileged(Native Method)
                         at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
                         at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
                         at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
                         at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
                         at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
                         at java.lang.Class.forName0(Native Method)
                         at java.lang.Class.forName(Class.java:242)
                         at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:42)
                         at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)
                         ... 16 more
                        Exception in thread "main" java.lang.NullPointerException
                         at Main.main(Main.java:106)[BidClient] : closing entity manager and factory...
                        


                        • 9. Re: Could not find datasource message in client JPA although
                          jaikiran pai Master

                           

                          Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory


                          The exception still shows this classnotfoundexception. How are you adding the jars to the classpath and how are you running the client?

                          • 10. Re: Could not find datasource message in client JPA although
                            ezani abdul halim Newbie

                            Ugh..this is quite frustating...8 full days already. I have to come up with a SSO-->LDAP-->JSF/Ajax-->SEAM-->ESB-->EJB-->JPA-->Database stack which includes jBPM + Drools by end of January.

                            Ok..getting back to the real world..at least the server side is running OK and the schema creation of the BIDDER table is working. It's only the client side that appears to be giving me problems. My plan is to get this Java Application client working and successfully inserting records and then change it into a session bean with @Session and then work on the JSF/SEAM UI front-end.

                            Ok...given that the server side is working..I'm now just tweaking and playing around with various combinations within the client application code.

                            I know that I don't have to create an entity manager in code because this annotated portion of code should inject me an entity manager binded to my persistent unit, BiddingTest :

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


                            True enough, I commented the code creating the EntityManagerFactory and the app still runs with no error.

                            I've gotten rid of the 'Unable to find datasource' error. Last time I did my EJB 2.1, I also had initial hiccup problems but putting in some properties code solved my problem.

                            Here's my new BidClient.java :-



                            My jndi.properties file for BidClient.java is in the main folder of the client module as follows :-

                            java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
                            java.naming.provider.url=jnp://localhost:1099
                            java.naming.factory.url.pkgs=org.jboss.naming.client
                            j2ee.clientName=BiddingClient
                            


                            My application.xml file :
                            <?xml version="1.0" encoding="UTF-8"?>
                            
                            <application xmlns="http://java.sun.com/xml/ns/j2ee"
                             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                             http://java.sun.com/xml/ns/j2ee/application_1_4.xsd"
                             version="1.4">
                            
                             <display-name>BiddingTestEAR</display-name>
                             <module>
                             <ejb>BiddingTest.jar</ejb>
                             </module>
                            </application>
                            


                            My application-client.xml file :
                            <?xml version="1.0" encoding="UTF-8"?>
                            
                            <application-client xmlns="http://java.sun.com/xml/ns/j2ee"
                             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                             http://java.sun.com/xml/ns/j2ee/application-client_1_4.xsd"
                             version="1.4">
                            
                             <display-name>OracleXE1_DS</display-name>
                            
                            </application-client>
                            


                            My persistence.xml file :
                            <?xml version="1.0" encoding="UTF-8"?>
                            <persistence>
                             <persistence-unit name="BiddingTest">
                             <jta-data-source>java:/OracleXE1_DS</jta-data-source>
                             <properties>
                             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
                             <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
                             </properties>
                             </persistence-unit>
                            </persistence>
                            


                            My oracle-ds.xml file :
                            <?xml version="1.0" encoding="UTF-8"?>
                            <datasources>
                             <local-tx-datasource>
                             <jndi-name>OracleXE1_DS</jndi-name>
                             <connection-url>jdbc:oracle:thin:system/system@localhost:1521:XE</connection-url>
                             <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
                             <user-name>system</user-name>
                             <password>system</password>
                             <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
                             <metadata>
                             <type-mapping>Oracle10g</type-mapping>
                             </metadata>
                             </local-tx-datasource>
                            </datasources>
                            


                            And finally my BidClient.java Java Application client program:
                            
                            
                            import javax.persistence.EntityManager;
                            import javax.persistence.EntityManagerFactory;
                            import javax.persistence.Persistence;
                            import javax.persistence.PersistenceContext;
                            
                            import org.apache.log4j.Level;
                            import org.apache.log4j.Logger;
                            import org.apache.log4j.ConsoleAppender;
                            import org.apache.log4j.SimpleLayout;
                            
                            import my.com.eperolehan.entities.Bidder;
                            
                            import javax.rmi.*;
                            import javax.sql.DataSource;
                            import javax.naming.InitialContext;
                            import javax.naming.NamingException;
                            
                            import java.sql.SQLException;
                            import java.util.*;
                            
                            
                            
                            public class BidClient {
                            
                            
                             private static EntityManagerFactory f;
                            
                             @PersistenceContext(unitName="BiddingTest")
                             private static EntityManager em;
                            
                            
                             public static void main(String[] args) throws NamingException {
                            
                            
                             Logger log = Logger.getLogger(BidClient.class);
                             log.setLevel(Level.WARN);
                            
                             SimpleLayout layout = new SimpleLayout();
                             ConsoleAppender appender = new ConsoleAppender(layout);
                            
                            
                             log.addAppender(appender);
                            
                            
                             try {
                            
                             Properties p = new Properties();
                             p.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                             p.put("java.naming.provider.url", "localhost:1099");
                            
                            
                             InitialContext ctx = new InitialContext( );
                             DataSource obj = (DataSource) ctx.lookup("java:OracleXE1_DS");
                            
                            
                             log.debug("[BidClient] : creating EntityManagerFactory...");
                             //f = Persistence.createEntityManagerFactory("BiddingTest");
                             log.debug("[BidClient] : Created.");
                             //em = f.createEntityManager();
                             log.debug("[BidClient] : Created.");
                            
                            
                             em.getTransaction().begin();
                            
                            
                             try {
                             log.debug("[BidClient] : creating a Bidder...");
                             Bidder bidder = new Bidder();
                            
                             bidder.setBidderName("Bidder No 1");
                             bidder.setBidSessionId(1);
                            
                            
                             em.persist(bidder);
                             em.getTransaction().commit();
                             log.debug("[BidClient] : Bidder created and commited.");
                            
                             } catch (Exception ex) {
                             em.getTransaction().rollback();
                             }
                            
                             log.debug("[BidClient] : Bidder person created and persisted successfully.");
                            
                             } catch (RuntimeException e) {
                            
                             e.printStackTrace();
                            
                             } finally {
                            
                            
                            
                             }
                            
                            
                             em.close();
                             //f.close();
                            
                             }
                            
                            
                            }
                            


                            And the console output :-
                            Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory ]
                             at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)
                             at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
                             at javax.naming.InitialContext.init(InitialContext.java:223)
                             at javax.naming.InitialContext.<init>(InitialContext.java:175)
                             at BidClient.main(BidClient.java:71)
                            Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory
                             at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
                             at java.security.AccessController.doPrivileged(Native Method)
                             at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
                             at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
                             at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
                             at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
                             at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
                             at java.lang.Class.forName0(Native Method)
                             at java.lang.Class.forName(Class.java:242)
                             at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:42)
                             at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)
                             ... 4 more
                            



                            Oh boy...I sure would like to move on from here...happy new year!

                            • 11. Re: Could not find datasource message in client JPA although
                              ezani abdul halim Newbie

                              The error appears to occur at this line :

                              InitialContext ic = new InitialContext();
                              


                              • 12. Re: Could not find datasource message in client JPA although
                                ezani abdul halim Newbie

                                Hi

                                I think I've narrowed down the possible JNDI naming problem to this few lines of code :-

                                Properties p = new Properties();
                                p.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                                p.put("java.naming.provider.url", "localhost:1099");
                                p.put("java.naming.factory.url.pkgs", "org.jboss.naming.client");
                                
                                InitialContext ctx = new InitialContext();
                                Bidder bidder = (Bidder) ctx.lookup(Bidder.class.getName());
                                //Object obj = ctx.lookup("java:OracleXE1_DS");
                                //DataSource obj = (DataSource) ctx.lookup("java:OracleXE1_DS");
                                



                                It has to be one the 3 lines below the initial context instantiation line. Perhaps if you could just give me some pointers what each of those 3 lines do? Thxs!

                                As per the code above, I'm still getting the naming error as before :-(

                                Oh..I forgot to post the code for the Bidder.java entity which is just a standard JPA entity class :-

                                package my.com.eperolehan.entities;
                                
                                import java.io.Serializable;
                                import java.lang.String;
                                import javax.persistence.*;
                                
                                /**
                                 * Entity implementation class for Entity: Bidder
                                 *
                                 */
                                @Entity
                                @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
                                @Table(name="BIDDER")
                                public class Bidder implements Serializable {
                                
                                
                                 @Id
                                 @GeneratedValue
                                 @Column(nullable=false)
                                 private int id;
                                 private int bidSessionId;
                                 private String bidderName;
                                 private static final long serialVersionUID = 1L;
                                
                                
                                 public Bidder() {
                                 super();
                                 }
                                 public int getId() {
                                 return this.id;
                                 }
                                
                                 public void setId(int id) {
                                 this.id = id;
                                 }
                                 public int getBidSessionId() {
                                 return this.bidSessionId;
                                 }
                                
                                 public void setBidSessionId(int bidSessionId) {
                                 this.bidSessionId = bidSessionId;
                                 }
                                 public String getBidderName() {
                                 return this.bidderName;
                                 }
                                
                                 public void setBidderName(String bidderName) {
                                 this.bidderName = bidderName;
                                 }
                                
                                 public String toString() {
                                
                                 StringBuffer BidderInfo = new StringBuffer();
                                
                                 BidderInfo.append("id");
                                 BidderInfo.append(" " );
                                 BidderInfo.append("BidSessionId");
                                 BidderInfo.append(" " );
                                 BidderInfo.append("BidderName");
                                 BidderInfo.append(" : " );
                                 BidderInfo.append(id);
                                 BidderInfo.append(" " );
                                 BidderInfo.append(bidSessionId);
                                 BidderInfo.append(" " );
                                 BidderInfo.append(bidderName);
                                 return BidderInfo.toString();
                                 }
                                
                                }
                                


                                • 13. Re: Could not find datasource message in client JPA although
                                  ezani abdul halim Newbie

                                  Ok...I think I'm getting very close to resolving this...please help me.

                                  This is some new info I found.

                                  The first part:

                                  I've changed my InitialContext code from :

                                   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");
                                   p.put("j2ee.clientName", "OracleXE1_DS");
                                  
                                  
                                  
                                   InitialContext ctx = new InitialContext();
                                  


                                  to

                                   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");
                                   p.put("j2ee.clientName", "OracleXE1_DS");
                                  
                                  
                                  
                                   InitialContext ctx = new InitialContext(p);
                                  



                                  The second part (probably the most important) :-

                                  I learnt that jboss-client.xml is very important and should be included in META-INF (together with application-client.xml) and it allows the client to be binded using the naming services.

                                  So my client module (or Eclipse project) is named BiddingClient and my client class is named BidClient.java.

                                  So is the contents of my jboss-config.xml file below correct and the way I am calling it (see below) ?

                                  jboss-client.xml:
                                  <?xml version='1.0' encoding='UTF-8' ?>
                                  
                                  <!DOCTYPE jboss-client PUBLIC
                                  "-//JBoss//DTD Application Client 3.2//EN"
                                  "http://www.jboss.org/j2ee/dtd/jboss-client_3_2.dtd">
                                  
                                  <jboss-client>
                                  <jndi-name>BiddingClient</jndi-name>
                                  </jboss-client>
                                  


                                  application-client.xml:
                                  <?xml version="1.0" encoding="UTF-8"?>
                                  
                                  <application-client xmlns="http://java.sun.com/xml/ns/j2ee"
                                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                                   http://java.sun.com/xml/ns/j2ee/application-client_1_4.xsd"
                                   version="1.4">
                                  
                                   <display-name>BiddingClientApp</display-name>
                                  
                                  </application-client>
                                  



                                  and the portion of the code in my BidClient.java that looks up the client JNDI name after getting the initial context is :-

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


                                  Is this correct ?

                                  • 14. Re: Could not find datasource message in client JPA although
                                    ezani abdul halim Newbie

                                    Ok..whatever name I'm putting in the lookup line of code and j2ee.clientName in jndi.properties is bouncing back with the error saying it is not bound.

                                    I am seeing the BiddingClient.jar in the directory of the EAR module. I have not added application-client.xml and jboss-client.xml to the EAR's META-INF. I have added it only to the client's META-INF which is a separate Java Application Client project. I wonder whether I should add application-client.xml and jboss-client.xml to the EAR's META-INF ?

                                    Please advise.

                                    1 2 Previous Next