title: Tomcat conection pooling for oracle DB using Jndi
body: <p>For my project  i want to use tomcat connection pooling for oracle DB using jndi and getting exception while executing the below code:</p>
  
  <p>This is the exception which i am getting:</p>
  
  <pre><code>  javax.naming.NameNotFoundException: Name java: is not bound in this Context
      at org.apache.naming.NamingContext.lookup(NamingContext.java:803)
      at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
      at javax.naming.InitialContext.lookup(InitialContext.java:392)
      at com.iton.dbcp.DBUtil.getConnection(DBUtil.java:28)
      at com.iton.dbcp.TestServlet.doGet(TestServlet.java:35)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
     icationFilterChain.java:252)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
    ilterChain.java:173)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
  alve.java:213)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
  alve.java:178)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
  ava:126)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
  ava:105)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
  ve.java:107)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
  a:148)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
  :869)
      at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.p
  rocessConnection(Http11BaseProtocol.java:664)
      at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpo
    int.java:527)
      at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFol
    lowerWorkerThread.java:80)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
  ool.java:684)
      at java.lang.Thread.run(Thread.java:662)
  </code></pre>
  
  <p>These are the files which i am using for the connection pool</p>
  
  <p>i was placed the below configuration in /conf/server.xml :</p>
  
  <pre><code>&lt;Resource name="jdbc/pooldb" auth="Container" type="javax.sql.DataSource"
   maxActive="5" maxIdle="2" maxWait="1000"                         factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
              driverClassName="oracle.jdbc.driver.OracleDriver"
              url="jdbc:oracle:thin:@localhost:1521:pooldb"/&gt;
  </code></pre>
  
  <p>DBUtil1.java:</p>
  
  <pre><code>package com.iton.dbcp;
  
  import java.sql.Connection;
  import java.sql.SQLException;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  import javax.sql.DataSource;
  
  public class DBUtil1 {
  
  public static Connection getConnection() throws NamingException, SQLException {
  
      Context initContext = new InitialContext();
      Context envContext = (Context) initContext.lookup("java:/comp/env");
      DataSource ds = null;
      Connection conn = null;
      try {
          ds = (DataSource) envContext.lookup("jdbc/pooldb");
      } catch (Exception e) {
          e.printStackTrace();
      }
      if (envContext == null) {
          System.out.println("envcontext is null");
      }
      if (ds == null) {
          System.out.println("datasource is null");
      }
      if (ds != null){
        conn = ds.getConnection("pooldb","pooldb");
      }
      return conn;
  }
   }
  </code></pre>
  
  <p>TestServlet.java:</p>
  
  <pre><code>package com.iton.dbcp;
  
  import java.io.IOException;
  import java.sql.Connection;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  import javax.naming.NamingException;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  
  public class TestServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  /**
   * @see HttpServlet#HttpServlet()
   */
  public TestServlet() {
      super();
      // TODO Auto-generated constructor stub
  }
  
  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws      ServletException, IOException {
  
      Connection con = null;
      try {
          con = DBUtil.getConnection();
          System.out.println("opened conncetion:::::::::: "+con);
          Statement stmt = con.createStatement();
          stmt.executeUpdate("create table iton(sno varchar(10), name varchar(10))");
          stmt.close();
          DBUtil.closeConncetion(con);
          System.out.println("Closeddddddddddddddd: "+con);
  
      } catch (NamingException e) {
          e.printStackTrace();
      } catch (SQLException e) {
          e.printStackTrace();
      }
     }
  </code></pre>
  
  <p>web.xml:
      
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
       TestApp</p>
  
  <pre><code>&lt;description&gt;Oracle Test App&lt;/description&gt;
  &lt;resource-ref&gt;
  &lt;description&gt;Oracle Datasource example&lt;/description&gt;
  &lt;res-ref-name&gt;jdbc/pooldb&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;servlet&gt;
  &lt;servlet-name&gt;abscd&lt;/servlet-name&gt;
  &lt;servlet-class&gt;com.iton.dbcp.TestServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
  &lt;servlet-name&gt;abscd&lt;/servlet-name&gt;
  &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>in /conf/context.xml:</p>
  
  <pre><code> &lt;!-- The contents of this file will be loaded for each web application --&gt;
   &lt;Context&gt;
  
  &lt;!-- Default set of monitored resources --&gt;
  &lt;WatchedResource&gt;WEB-INF/web.xml&lt;/WatchedResource&gt;
  
  &lt;!-- Uncomment this to disable session persistence across Tomcat restarts --&gt;
  &lt;!--
  &lt;Manager pathname="" /&gt;
  --&gt;
  
  &lt;ResourceLink global="jdbc/pooldb" name="jdbc/pooldb" type="javax.sql.DataSource"/&gt; 
  &lt;/Context&gt;
  </code></pre>
  
  <p>please resolve this issue</p>