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

    ANSWER: JNDI and Datasource and EJB issue

    Jeff Andrews Newbie


      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.