title: referring Tomcat JNDI datasource in persistence.xml
body: <p>in server.xml I've defined global resource (I'm using <strong>Tomcat 6</strong>):</p>
  
  <pre><code>&lt;GlobalNamingResources&gt;
     &lt;Resource name="jdbc/myds" auth="Container"
               type="javax.sql.DataSource"
               maxActive="10" maxIdle="3" maxWait="10000"
               username="sa"  password=""
               driverClassName="org.h2.Driver"
               url="jdbc:h2:~/.myds/data/db"
     /&gt;
  &lt;/GlobalNamingResources&gt;
  </code></pre>
  
  <p>I see in catalina.out that this is bound, so I suppose it's OK.</p>
  
  <p>In my web app I have the link to the datasource, I'm not sure it's OK:</p>
  
  <pre><code>&lt;Context&gt;    
   &lt;ResourceLink global='jdbc/myds' name='jdbc/myds' type="javax.sql.Datasource"/&gt;    
  &lt;/Context&gt;
  </code></pre>
  
  <p>and in application there is persistence.xml:</p>
  
  <pre><code>&lt;persistence 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"
               version="2.0"&gt;
    &lt;persistence-unit name="oam" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;non-jta-data-source&gt;jdbc/myds&lt;/non-jta-data-source&gt;
      &lt;!-- class definitions here, nothing else --&gt;
  
      &lt;properties&gt;
        &lt;property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/&gt;
      &lt;/properties&gt;
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>It should be OK, but most probably this or the ResourceLink definition is wrong because I'm getting:</p>
  
  <blockquote>
    <p>javax.naming.NameNotFoundException:
    Name jdbc is not bound in this Context</p>
  </blockquote>
  
  <p>What's wrong and why this does not work? </p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I've tried to get the datasource directly: </p>
  
  <pre><code>public class WebAppListener implements ServletContextListener
  {
      // ServletContextListener interface - start
      public void contextInitialized(ServletContextEvent sce)
      {
          try
          {
              Context initCtx = new InitialContext();
              Context envCtx = (Context) initCtx.lookup("java:comp/env");
              DataSource ds = (DataSource)
              envCtx.lookup("jdbc/myds");
          }
          catch (NamingException ex)
          {
              System.out.println("!!!! Got NamingException:");
              ex.printStackTrace(System.out);
          }
      }
  
      public void contextDestroyed(ServletContextEvent sce) { }
  
  }
  </code></pre>
  
  <p>my web.xml:</p>
  
  <pre><code>  &lt;listener&gt;
      &lt;display-name&gt;Listener&lt;/display-name&gt;
      &lt;listener-class&gt;WebAppListener&lt;/listener-class&gt;
    &lt;/listener&gt;
  </code></pre>
  
  <p>Still getting the same error although I see the datasource in JMX console when I connect to the Tomcat
  (Catalina - Datasource - javax.sql.Datasource = "jdbc/myds" : 
  ObjectName = Catalina:type=DataSource,class=javax.sql.DataSource,name="jdbc/myds". )</p>