2 Replies Latest reply on May 10, 2007 10:48 AM by swjackson

    Problrms getting Datasource connection whith DAO and EJB

    Carlos Daniel Munoz Idarraga Newbie

      Inside the same project I'm using both EJB 3.0 (with configuration in persistence.xml) and DAO with JDBC. I don't have problems accessing objects with EJB beacuse they are on a particular module, but I can't access any object with JDBC.
      I got configured my oracle-xa-ds.xml as follow:
      <xa-datasource>
      <jndi-name>fmarketdb</jndi-name> . . .
      for EJB and
      <xa-datasource>
      <jndi-name>it</jndi-name> . . .
      for JDBC.
      Jboss Application Server (jboss-4.0.3_SP1) inits the datasource without problems.

      My jboss-web.xml:
      <jboss-web>
      <resource-ref>
      <res-ref-name>jdbc/it</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <jndi-name>java:/it</jndi-name>
      </resource-ref>
      </jboss-web>


      My web.xml:
      <jboss-web>
      <resource-ref>
      <res-ref-name>jdbc/it</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <jndi-name>java:/it</jndi-name>
      </resource-ref>
      </jboss-web>


      My Java source (jdk1.5.0_08):
      protected Connection getConnection() throws Exception {
      Context initCtx = new InitialContext();
      Context envCtx = (Context) initCtx.lookup("java:comp/env");
      DataSource ds = (DataSource)envCtx.lookup("jdbc/it");
      Connection conn = ds.getConnection();
      conn.setAutoCommit(false);
      return conn;
      }


      When I try to access an object through JDBC, I got the next error:
      java.lang.NullPointerException at com.ceiba.corporativo.root.bo.BussinessLogicAbs.getConnection(BussinessLogicAbs.java:57)
      and this because envCtx.lookup("jdbc/it") returns null and I don't understand why? Those are independent modules.
      Can somebody help me please?
      Does affect EJB configuration inside Jboss?

        • 1. Re: Problrms getting Datasource connection whith DAO and EJB
          swjackson Newbie

          If you are using EJB3 I recommend using a persistence.xml file.

          such as...


          <persistence-unit name="metadata">
          <jta-data-source>java:/it</jta-data-source>
          org.hibernate.ejb.HibernatePersistence







          </persistence-unit>


          In your ejb instead of trying to create a jdbc connection use the annotation in your ejb
          @PersistanceUnit(unit="metadata")
          EntityManagerFactory factory

          in your method you can access the query object such as

          EntityManager em = factory.createEntityManager
          Query query = em.whatYouNeedToDo...

          Or if you need more advance features of the Hibernate Query object
          //in your ejb or where you need it.
          private EntityManagerFactory factory = new HibernatePersistence().createEntityManagerFactory(GUI, new HashMap());

          in your method
          Session session = session = ((HibernateEntityManagerFactory)factory).getSessionFactory().getCurrentSession();

          HTH

          • 2. Re: Problrms getting Datasource connection whith DAO and EJB
            swjackson Newbie

            my persistence xml file didn't make it...

            <persistence>
            
             <persistence-unit name="metadata">
             <jta-data-source>java:/it</jta-data-source>
             <provider>org.hibernate.ejb.HibernatePersistence</provider>
             <properties>
             <property name="hibernate.dialect" value="org.hibernate.dialect.SybaseDialect"/>
             <property name="hibernate.show_sql" value="true"/>
             <property name="hibernate.current_session_context_class" value="org.hibernate.context.JTASessionContext"/>
             <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory"/>
             <property name="hibernate.transaction.manager_lookup_class"
             value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
             </properties>
             </persistence-unit>
            
            
            </persistence>