title: resource reference in EJB3
body: <p>May be this is a common question, I am trying to create an Stateless session EJB 3.0 with resource reference for data source in Websphere 7.0. </p>
  
  <p>The following code is working:</p>
  
  <pre class="lang-java prettyprint-override"><code>package com;
  import javax.ejb.Stateless;
  import javax.annotation.Resource;
  import javax.sql.DataSource;
  @Stateless
  public class Test1 implements Test1Remote {
   @Resource DataSource ds;
   public Test1() {}
   public boolean testDs() {
    boolean valid = true;
    try {
     ds.getConnection();
    } catch (Exception e) {
     e.printStackTrace();
     valid = true;
    }
    return valid;
   }
  }
  </code></pre>
  
  <p><br/></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ejb-jar-bnd
      xmlns="http://websphere.ibm.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee 
      http://websphere.ibm.com/xml/ns/javaee/ibm-ejb-jar-bnd_1_0.xsd" version="1.0"&gt;
  
      &lt;session name="Test1"&gt;
       &lt;interface class="com.Test1Remote" binding-name="Test1"/&gt;
       &lt;resource-ref name="com.Test1/ds" binding-name="jdbc/myDataSource"&gt;&lt;/resource-ref&gt;
      &lt;/session&gt;
  &lt;/ejb-jar-bnd&gt;
  </code></pre>
  
  <p>The following code is not working and giving me excpetion</p>
  
  <pre><code>javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".
  </code></pre>
  
  <p>But not sure how to proceed when the Datasource needs to be looked up in initialContext out side of EJB. </p>
  
  <pre class="lang-java prettyprint-override"><code>package com;
  import javax.ejb.Stateless;
  @Stateless
  public class Test1 implements Test1Remote {
      public Test1() {
      }
      public boolean testDs(){
       boolean valid = true;
       try {
           MyConnection ds = new MyConnection();
           ds.getConnection();
       } catch (Exception e) {
          e.printStackTrace();
          valid = true;
      }
      return valid;
      }
  }
  
  package com;
  import java.sql.Connection;
  import java.sql.SQLException;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  import javax.sql.DataSource;
  
  
  public class MyConnection{
      private DataSource ds;
      public Object getConnection() throws Exception  {
   try {
      final InitialContextinitCtx = new InitialContext();
      ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/myDataSource");
      return ds.getConnection();
      } catch (NamingException e) {
       e.printStackTrace();
       throw e;
      } catch (SQLException e) {
       e.printStackTrace();
       throw e;
      }
   }
  }
  
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ejb-jar-bnd
      xmlns="http://websphere.ibm.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee 
      http://websphere.ibm.com/xml/ns/javaee/ibm-ejb-jar-bnd_1_0.xsd" version="1.0"&gt;
  
      &lt;session name="Test1"&gt;
       &lt;interface class="com.Test1Remote" binding-name="Test1"/&gt;
      &lt;/session&gt;
  &lt;/ejb-jar-bnd&gt;
  </code></pre>
  
  <p>If I dont use "java:comp/env/" in data source lookup then it works fine, but that is not a good practice.</p>
  
  <p>UPDATE: Thanks for the replies. My goal is to replace an existing EJB 2.1 to EJB 3.0.        The current EJB 2.1 is designed similar to code snippet 2 so I wrote this test. EJB 2.1 is functioning with the resource defined out side of the ejb (but has the resource reference declared in ejb-jar.xml). If I dont follow the same approach then it will have a huge impact which I want to avoid.</p>