2 Replies Latest reply on Feb 11, 2003 9:36 AM by cimetgiu

    ANSWER: JNDI and Datasource and EJB issue

    qjack


      I've seen a lot of postings regarding using the
      j2ee ENC naming context to get to a datasource. With
      much chagrin I've never seen it answered completely or
      with example. I'm posting this just because I haven't
      seen it in these forums, not to say that it hasn't been
      posted.

      Take a Product session bean. Inside the bean impl, a
      need to connect to the data source is required. I need
      to make sure my jboss.xml references the resource and
      I must have a matching resource ref in the ejb-jar.xml

      This would be in the ejb-jar.xml

      <ejb-name>Product</ejb-name>

      com.broadsource.onesource.ejb.ProductRemoteHome


      com.broadsource.onesource.ejb.ProductRemote

      <ejb-class>
      com.broadsource.onesource.ejb.ProductSessionBean
      </ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>
      <resource-ref>
      <res-ref-name>
      jdbc/OneSourceMainDB
      </res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
      </resource-ref>


      This would be in the jboss.xml

      <ejb-name>Product</ejb-name>
      <jndi-name>ejb/ProductRemoteHome</jndi-name>
      <resource-ref>
      <res-ref-name>jdbc/OneSourceMainDB</res-ref-name>
      <jndi-name>java:/OneSourceMainDB</jndi-name>
      </resource-ref>


      Within the bean impl I can do this:
      Context ctx = new InitialContext();
      Context tmp =
      (Context)ctx.lookup("java:comp/env/jdbc");
      DataSource ds =
      (DataSource)tmp.lookup("OneSourceMainDB");

      Connection conn = ds.getConnection();

      And with that I have my connection.

      Hope this helps you with the jndi enc issue.

        • 1. Re: ANSWER: JNDI and Datasource and EJB issue
          stefano_fornari

          This is Cool!

          But still I have one doubt: as per J2EE specification, I should be able to look up a datasource like this:

          Context ctx = new InitialContext();
          DataSource ds =
          (DataSource)ctx.lookup("jdbc/OneSourceMainDB");

          Connection conn = ds.getConnection();

          Which I am not able to with JBoss 3.0.6.
          Any advice?

          Thanks in advance.

          Stefano

          • 2. Re: ANSWER: JNDI and Datasource and EJB issue
            cimetgiu

            thank you for the example,
            can I ask you another question:
            how I had to configure my oracle-service.xml?
            It's right that I had to copy it into deploy directory?
            and that's all I have to do for connecting to db?
            If I want to read a table of db, what I have to write in
            my EJB?

            Thanks and scuse me for the questions.