title: Lookup jndi resources WebSphere 8.5, Spring 3.1.3, Hibernate 4.1.10 EJB 3
body: <p>In my application I need to lookup jndi resources for database connection.</p>
  
  <p>I have an EAR which contain my EJB project with remote and stateless ejb: <code>ABean.jar</code></p>
  
  <p>Reading <a href="http://www.ibm.com/developerworks/websphere/techjournal/0609_alcott/0609_alcott.html" rel="nofollow">Using Spring and Hibernate with WebSphere Application Server</a> I defined
  in the <code>application.xml</code> of EAR project:</p>
  
  <pre><code>&lt;module&gt;
      &lt;ejb&gt;ABean.jar&lt;/ejb&gt;
  
  &lt;/module&gt;
  
  &lt;resource-ref&gt;
      &lt;res-ref-name&gt;jdbc/b&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;res-sharing-scope&gt;Shareable&lt;/res-sharing-scope&gt;
  &lt;/resource-ref&gt;
  </code></pre>
  
  <p>In my spring context:</p>
  
  <pre><code>&lt;jee:jndi-lookup
      id="aDataSource" 
      jndi-name="jdbc/b" 
      cache="true" 
      expected-type="javax.sql.DataSource"/&gt;
  
  &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="persistenceXmlLocation" value="META-INF/persistence.xml"/&gt;
      &lt;property name="dataSource" ref="aDataSource" /&gt;
      &lt;property name="persistenceUnitName" value="APU"/&gt;
      &lt;property name="jpaProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.DB2390Dialect&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
      &lt;property name="jpaDialect"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /&gt;
      &lt;/property&gt; 
  &lt;/bean&gt;
  </code></pre>
  
  <p>Now, if I install application by WebSphere console, it ask to me to assign the jndi resource to <code>jdbc/b</code> (and it is ok), but if I try to call an ejb, a <code>NameNotFoundException</code> is throw because <code>jdbc/b</code> is not found!</p>
  
  <p>Reading better the previous link i see:</p>
  
  <blockquote>
    <p>For EJB JAR files, the same resource-ref should be declared in each EJB that needs to access the data source.</p>
  </blockquote>
  
  <p>so I try to define resource references into my <code>ejb-jar.xml</code></p>
  
  <pre><code>&lt;enterprise-beans&gt;
  &lt;session&gt;
     &lt;ejb-name&gt;TestServiceBean&lt;/ejb-name&gt;
     &lt;ejb-class&gt;it.test.TestServiceBean&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&gt;
          &lt;res-ref-name&gt;jdbc/b&lt;/res-ref-name&gt;         
          &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;   
     &lt;/resource-ref&gt;
   &lt;/session&gt;
   &lt;/enterprise-beans&gt;
  </code></pre>
  
  <p>I try to install again (I also assign the jndi resource to <code>jdbc/b</code>) and the call to the ejb run successfully, but if I have n ejb, do I need to assign n-times the same jndi resource to the n ejb resource-ref for jdbc/b?</p>
  
  <p>What is wrong with my configuration? Isn't it possible "point" to the same <code>resource-ref</code> from all ejb? Do you have a complete example?</p>