title: Datasource JNDI configuration in Tomcat 7.0 for Hibernate
body: <p>When Datasource is configured in (tomcat 7) server.xml or in Context.xml, hibernate is not able to identify JNDI and throwing below excpetion but same is working fine when I tried to access datasource using JNDI in the code. What am doing wrong</p>
  
  <p><strong>Below is my configuration in hibernate.properties</strong></p>
  
  <pre><code>hibernate.connection.driver_class com.mysql.jdbc.Driver
  hibernate.connection.pool_size 10
  hibernate.dialect org.hibernate.dialect.MySQL5Dialect
  hibernate.connection.datasource java:comp/env/jdbc/employee
  hibernate.show_sql true
  hibernate.format_sql true
  </code></pre>
  
  <p>in server.xml</p>
  
  <pre><code> &lt;Host name="localhost"  appBase="webapps"
          unpackWARs="true" autoDeploy="true"&gt;
  
      &lt;!-- SingleSignOn valve, share authentication between web applications
           Documentation at: /docs/config/valve.html --&gt;
      &lt;!--
      &lt;Valve className="org.apache.catalina.authenticator.SingleSignOn" /&gt;
      --&gt;
  
      &lt;!-- Access log processes all example.
           Documentation at: /docs/config/valve.html
           Note: The pattern used is equivalent to using pattern="common" --&gt;
      &lt;Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
             prefix="localhost_access_log." suffix=".txt"
             pattern="%h %l %u %t &amp;quot;%r&amp;quot; %s %b" resolveHosts="false"/&gt;
      **&lt;Context docBase="hibernate" path="/hibernate"&gt;
              &lt;Resource name="jdbc/employee" auth="Container"
            type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:45000/hibernatetest"
            username="user" password="hibernate" maxActive="20" maxIdle="10"
            maxWait="-1" removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true"/&gt;
          &lt;/Context&gt;**
    &lt;/Host&gt;
  </code></pre>
  
  <p>In Context.xml file</p>
  
  <pre><code>    **&lt;Context docBase="hibernate" path="/hibernate"&gt;
          &lt;Resource name="jdbc/employee" auth="Container"
            type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:45000/hibernatetest"
            username="user" password="hibernate" maxActive="20" maxIdle="10"
            maxWait="-1" removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true"/&gt;
      &lt;/Context&gt;**
  </code></pre>
  
  <p>Finally below is my web.xml file</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
      &lt;!DOCTYPE web-app PUBLIC
      "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
      "http://java.sun.com/dtd/web-app_2_3.dtd"&gt;
      **&lt;web-app &gt;
          &lt;servlet&gt;
              &lt;servlet-name&gt;employee&lt;/servlet-name&gt;
              &lt;servlet-class&gt;edu.experiments.web.EmployeViewer&lt;/servlet-class&gt;
          &lt;/servlet&gt;
          &lt;servlet-mapping&gt;
              &lt;servlet-name&gt;employee&lt;/servlet-name&gt;
              &lt;url-pattern&gt;/employee&lt;/url-pattern&gt;        
          &lt;/servlet-mapping&gt;
  
          &lt;resource-ref&gt;
              &lt;description&gt;DB Connection&lt;/description&gt;
              &lt;res-ref-name&gt;jdbc/employee&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;/web-app&gt;**
  
  
  org.hibernate.service.jndi.JndiException: Unable to lookup JNDI name [java:comp/env/jdbc/employee]
  </code></pre>