Version 8

    How do I create a resource-ref?

     

    Resource references are created in your deployment descriptors. There is a standard component in ejb-jar.xml or web.xml and the actual jndi binding in jboss.xml or jboss-web.xml

     

    This creates an ENC (enterprise naming context) entry which can be retrieved using:

    new InitialContext().lookup("java:comp/env/jdbc/DataSource");
    

     

    Further information on this basic feature can be found in any good EJB book, the JBoss Getting Started Guide or the JBoss Admin Book.

     

    Standard Part (ejb-jar.xml shown)--

     

    <ejb-jar>
    <enterprise-beans>
    <session>
        <ejb-name>Whatever</ejb-name>
    ...
       <resource-ref>
          <description>My DataSource</description>
          <res-ref-name>jdbc/DataSource</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
       </resource-ref>
    </session>
    </enterprise-beans>
    </ejb-jar>
    

     

    JBoss Part (jboss.xml shown)

     

    <jboss>
    <enterprise-beans>
    <session>
        <ejb-name>Whatever</ejb-name>
    ...
       <resource-ref>
          <res-ref-name>jdbc/DataSource</res-ref-name>
          <jndi-name>java:/MySQLDS</jndi-name>
       </resource-ref>
    </session>
    </enterprise-beans>
    </jboss>
    

     

    Frequent Mistakes

     

    • The

      ejb-name

      and

      res-ref-name

      must match across the two files

    • The configuration is per EJB not the whole EJB archive

    • Forcing remote jndi access meaning the java: namespace is not visible, e.g.

    Properties props = new Properties();
    ...
    props.put(javax.naming.Context.PROVIDER_URL,"jnp://localhost:1099");
    IntialContext context = new InitialContext(props);
    
    • If you don't specify a

      jndi-name

      for a DataSource

      resource-ref

      it will use

      java:/DefaultDS

       

     

    Related