title: How to map a DataSource Resource Ref Name to JNDI Name in EJB 2.x in WildFly?
body: <p>How to map a DataSource Resource Ref Name to a JNDI Name in EJB in WildFly? </p>
  
  <p>I know that in JBoss 4.2.3 it is done in <code>jboss.xml</code> but it is not working in WildFly. </p>
  
  <p>Here is my test: </p>
  
  <p>I am working with Java 1.7, XDoclet 1.2.3, WildFly 8.2.1.Final, Dynamic Web Module 2.5, EJB 2.1 in Eclipse Luna. </p>
  
  <p>In WildFly I have a DataSource named <code>SchedulerDS</code> whose JNDI name is <code>java:jboss/datasources/SchedulerDS</code>. It is using jTDS JDBC Driver and connecting to a MS SQL Server 2008 R2 database. </p>
  
  <p>In Eclipse I created these projects: </p>
  
  <ol>
  <li>an Enterprise Application project named Z01_EAR.</li>
  <li>a Dynamic Web Project named Z01_WAR.</li>
  <li>a EJB Project named Z01_EJB.</li>
  <li>a EJB Client Project named Z01_EJBClient.</li>
  </ol>
  
  <p>In Z01_EJB project, I create a XDoclet Stateless Session Bean (EJB 2.1) named <code>SLS01</code>. In it I define a DataSource Resource Ref named <code>jdbc/chumbo</code>. And I define its mapping to JNDI Name <code>java:jboss/datasources/SchedulerDS</code>.</p>
  
  <p>This is the <code>ejb-jar.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ejb-jar id="ejb-jar_1" 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/ejb-jar_2_1.xsd" version="2.1"&gt;
     &lt;description&gt;&lt;![CDATA[Z01_EJB generated by eclipse wtp xdoclet extension.]]&gt;&lt;/description&gt;
     &lt;display-name&gt;Z01_EJB&lt;/display-name&gt;
     &lt;enterprise-beans&gt;
        &lt;session id="Session_SLS01"&gt;
           &lt;description&gt;&lt;![CDATA[An EJB named SLS01]]&gt;&lt;/description&gt;
           &lt;display-name&gt;SLS01&lt;/display-name&gt;
           &lt;ejb-name&gt;SLS01&lt;/ejb-name&gt;
           &lt;home&gt;com.z01.ejb.SLS01Home&lt;/home&gt;
           &lt;remote&gt;com.z01.ejb.SLS01&lt;/remote&gt;
           &lt;local-home&gt;com.z01.ejb.SLS01LocalHome&lt;/local-home&gt;
           &lt;local&gt;com.z01.ejb.SLS01Local&lt;/local&gt;
           &lt;ejb-class&gt;com.z01.ejb.SLS01Session&lt;/ejb-class&gt;
           &lt;session-type&gt;Stateless&lt;/session-type&gt;
           &lt;transaction-type&gt;Container&lt;/transaction-type&gt;
           &lt;resource-ref id="ResRef_1"&gt;
              &lt;res-ref-name&gt;jdbc/chumbo&lt;/res-ref-name&gt;
              &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
              &lt;res-auth&gt;Container&lt;/res-auth&gt;
           &lt;/resource-ref&gt;
        &lt;/session&gt;
     &lt;/enterprise-beans&gt;
     &lt;assembly-descriptor id="AssemblyDescriptor_1"&gt;
     &lt;/assembly-descriptor&gt;
     &lt;ejb-client-jar&gt;Z01_EJBClient.jar&lt;/ejb-client-jar&gt;
  &lt;/ejb-jar&gt;
  </code></pre>
  
  <p>This is the <code>jboss.xml</code>:  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 4.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd"&gt;
  &lt;jboss&gt;
     &lt;enterprise-beans&gt;
        &lt;session&gt;
           &lt;ejb-name&gt;SLS01&lt;/ejb-name&gt;
           &lt;jndi-name&gt;SLS01&lt;/jndi-name&gt;
           &lt;local-jndi-name&gt;SLS01Local&lt;/local-jndi-name&gt;
           &lt;resource-ref&gt;
              &lt;res-ref-name&gt;jdbc/chumbo&lt;/res-ref-name&gt;
              &lt;jndi-name&gt;java:jboss/datasources/SchedulerDS&lt;/jndi-name&gt;
           &lt;/resource-ref&gt;
          &lt;method-attributes&gt;
          &lt;/method-attributes&gt;
        &lt;/session&gt;
     &lt;/enterprise-beans&gt;
     &lt;assembly-descriptor&gt;
     &lt;/assembly-descriptor&gt;
     &lt;resource-managers&gt;
     &lt;/resource-managers&gt;
  &lt;/jboss&gt;
  </code></pre>
  
  <p>In EJB I wrote a method to test </p>
  
  <pre><code>public void testRetrieve() {
  
      String dataSourceLookupString = null;
      InitialContext initialContext = null;
      DataSource dataSource = null;       
      Connection connection = null;
      String sql = null;
      Statement statement = null;
      ResultSet resultSet = null;
      long employeeId = -1;
  
  
      try {
          dataSourceLookupString = "java:comp/env/jdbc/chumbo";           
          initialContext = new InitialContext();
          dataSource = (DataSource) initialContext.lookup(dataSourceLookupString);
          connection = dataSource.getConnection();
          sql = "select top 1 employee_id from employee";
          statement = connection.createStatement();           
          resultSet = statement.executeQuery(sql);            
          if (resultSet != null &amp;&amp; resultSet.next()) {
              employeeId = resultSet.getLong(1);
              System.out.println("testRetrieve(): employeeId=" + employeeId);
          }           
      } catch (NamingException e) {
          e.printStackTrace();            
      } catch (SQLException e) {
          e.printStackTrace();
      } finally {
          try {
              if (resultSet != null) {
                  resultSet.close();
              }
          } catch (SQLException e) {
              e.printStackTrace();
          }
  
          try {
              if (statement != null) {
                  statement.close();
              }
          } catch (SQLException e) {
              e.printStackTrace();
          }
  
          try {
              if (connection != null) {
                  connection.close();
              }
          } catch (SQLException e) {
              e.printStackTrace();                
          }
      }       
  }
  </code></pre>
  
  <p>It did not work and I got this error:</p>
  
  <blockquote>
    <p>2015-12-14 18:54:50,403 ERROR [stderr] (MSC service thread 1-6)
    org.h2.jdbc.JdbcSQLException: Table "EMPLOYEE" not found; SQL
    statement: 2015-12-14 18:54:50,403 ERROR [stderr] (MSC service thread
    1-6) select top 1 employee_id from employee [42102-173] 2015-12-14
    18:54:50,403 ERROR [stderr] (MSC service thread 1-6)  at
    org.h2.message.DbException.getJdbcSQLException(DbException.java:331)
    2015-12-14 18:54:50,403 ERROR [stderr] (MSC service thread 1-6)   at
    org.h2.message.DbException.get(DbException.java:171) 2015-12-14
    18:54:50,403 ERROR [stderr] (MSC service thread 1-6)  at
    org.h2.message.DbException.get(DbException.java:148) 2015-12-14
    18:54:50,403 ERROR [stderr] (MSC service thread 1-6)  at
    org.h2.command.Parser.readTableOrView(Parser.java:4864) 2015-12-14
    18:54:50,403 ERROR [stderr] (MSC service thread 1-6)  at
    org.h2.command.Parser.readTableFilter(Parser.java:1107) 2015-12-14
    18:54:50,403 ERROR [stderr] (MSC service thread 1-6)  at
    org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1713)
    2015-12-14 18:54:50,403 ERROR [stderr] (MSC service thread 1-6)   at
    org.h2.command.Parser.parseSelectSimple(Parser.java:1821) 2015-12-14
    18:54:50,403 ERROR [stderr] (MSC service thread 1-6)  at
    org.h2.command.Parser.parseSelectSub(Parser.java:1707) 2015-12-14
    18:54:50,403 ERROR [stderr] (MSC service thread 1-6)  at
    org.h2.command.Parser.parseSelectUnion(Parser.java:1550) 2015-12-14
    18:54:50,403 ERROR [stderr] (MSC service thread 1-6)  at
    org.h2.command.Parser.parseSelect(Parser.java:1538) 2015-12-14
    18:54:50,403 ERROR [stderr] (MSC service thread 1-6)  at
    org.h2.command.Parser.parsePrepared(Parser.java:405) 2015-12-14
    18:54:50,403 ERROR [stderr] (MSC service thread 1-6)  at
    org.h2.command.Parser.parse(Parser.java:279) 2015-12-14 18:54:50,419
    ERROR [stderr] (MSC service thread 1-6)   at
    org.h2.command.Parser.parse(Parser.java:251) 2015-12-14 18:54:50,419
    ERROR [stderr] (MSC service thread 1-6)   at
    org.h2.command.Parser.prepareCommand(Parser.java:218) 2015-12-14
    18:54:50,419 ERROR [stderr] (MSC service thread 1-6)  at
    org.h2.engine.Session.prepareLocal(Session.java:428) 2015-12-14
    18:54:50,419 ERROR [stderr] (MSC service thread 1-6)  at
    org.h2.engine.Session.prepareCommand(Session.java:377) 2015-12-14
    18:54:50,419 ERROR [stderr] (MSC service thread 1-6)  at
    org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1138)
    2015-12-14 18:54:50,419 ERROR [stderr] (MSC service thread 1-6)   at
    org.h2.jdbc.JdbcStatement.executeQuery(JdbcStatement.java:72)
    2015-12-14 18:54:50,419 ERROR [stderr] (MSC service thread 1-6)   at
    org.jboss.jca.adapters.jdbc.WrappedStatement.executeQuery(WrappedStatement.java:344)
    2015-12-14 18:54:50,419 ERROR [stderr] (MSC service thread 1-6)   at
    com.z01.ejb.SLS01Bean.testRetrieve(SLS01Bean.java:197)</p>
  </blockquote>
  
  <p>Now I change my code to lookup to JNDI Name directly:</p>
  
  <pre><code>dataSourceLookupString = "java:jboss/datasources/SchedulerDS";
  </code></pre>
  
  <p>And it worked perfect. So this means <code>jboss.xml</code> is not working in my case. How can I map a DataSource Resource Ref Name to a JNDI Name in this case?</p>
  
  <p>Thanks</p>