title: Cannot call JNDI resource in persistence.xml file
body: <p>Although it seems many users faced the same problem, I have to post my own issue, taking in account that for now, no suggestion from those already present worked for me. Or maybe am I missing something?</p>
  
  <p>Here is my <code>persistence.xml</code> file (located uner <code>resources/META-INF/</code> directory, in a maven project):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd "&gt;
      &lt;persistence-unit name="brass-unit" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
      &lt;properties&gt;
                  &lt;property name="hibernate.connection.datasource" value="java:/comp/env/jdbc/BrassDB"/&gt; 
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p></p>
  
  <p>under <code>resources/META-INF</code> I also have <code>context.xml</code> file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Context&gt;
      &lt;Resource name="jdbc/BrassDB" auth="Container" type="javax.sql.DataSource" 
                driverClassName="org.postgresql.Driver" 
                url="jdbc:postgresql://localhost:5432/mydb" username="usr" password="pwd" 
                maxActive="20" maxIdle="10" maxWait="-1"/&gt;
  &lt;/Context&gt;
  </code></pre>
  
  <p>In the <code>web.xml</code> file I have:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"&gt;
  &lt;display-name&gt;brass_directory_business&lt;/display-name&gt;
  &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt; 
  &lt;resource-ref&gt;
      &lt;description&gt;db connection&lt;/description&gt;
      &lt;res-ref-name&gt;jdbc/BrassDB&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;
  </code></pre>
  
  <p></p>
  
  <p>Note on web.xml: sometimes I commented <code>resource-ref</code> tag, but the same behavior results.</p>
  
  <p>Now, in my servlet, when I run this, everything works fine:</p>
  
  <pre><code>InitialContext ctx = null;
  try {
      ctx = new InitialContext();
      if (ctx == null) {
          throw new Exception("no context!");
      }
      DataSource ds = (DataSource)ctx.lookup("java:/comp/env/jdbc/BrassDB");
      if (ds == null) {
          throw new Exception("data source not found!");
      }
      Connection conn = ds.getConnection();
      ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM person");
      int n = 0;
      while (rs.next()) {
          n++;
      }
      System.out.println("rs length is: " + n);
  } catch (Exception e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>But when I run this:</p>
  
  <pre><code>EntityManagerFactory emf = Persistence.createEntityManagerFactory("brass-unit");
  EntityManager em = emf.createEntityManager();       
  em.createNamedQuery("Person.findAll", Person.class).getResultList();
  </code></pre>
  
  <p>I get this exception:</p>
  
  <pre><code>Mar 04, 2014 4:48:56 PM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [com.banco.Test] in context with path [/brass_directory_business] threw exception
  javax.persistence.PersistenceException: Unable to build entity manager factory
      at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:81)
      at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
      at com.banco.Test.doPost(Test.java:34)
      at com.banco.Test.doGet(Test.java:27)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:744)
  Caused by: org.hibernate.engine.jndi.JndiException: Unable to lookup JNDI name [java:/comp/env/jdbc/BrassDB]
      at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:117)
      at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:115)
      at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
      at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
      at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
      at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
      at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)
      at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:399)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
      at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:73)
      ... 26 more
  Caused by: javax.naming.NameNotFoundException: Name [java:/comp/env/jdbc/BrassDB] is not bound in this Context. Unable to find [java:].
      at org.apache.naming.NamingContext.lookup(NamingContext.java:819)
      at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
      at javax.naming.InitialContext.lookup(InitialContext.java:415)
      at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:114)
      ... 42 more
  </code></pre>
  
  <p>My <code>pom.xml</code> looks like:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      //......
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;4.3.1.Final&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
              &lt;version&gt;4.3.1.Final&lt;/version&gt;
          &lt;/dependency&gt;
          //...
      &lt;/dependencies&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
          //...
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>I'm using tomcat 7.0.52 as server.
  Does anyone have any suggestion how to solve my issue?</p>